Add "continued" text in the header in word doc using java

Hi Team,

I needed a functionality where I can add “continued” keyword in the header. I know I can do that by having sections in word doc and using “DifferentFirstPageHeaderFooter” option. But I want “continued” word in the doc where it will be bounded to “titles” to be put in header. For more details I have added a sample doc file.
continued word added.docx (17.4 KB)

You can see in the sample file how “Note 1 continued” header is added on the second page where it continued its content and also “Note 2 continued” header is added on the third page there its content continued.

I am iterating list of beans and adding contents of beans in the word doc. Each bean contains value in below format:

Bean:
{
title : html formatted string,
content : html formatted string
}

Please let me know if this is doable.

Regards

@amestry29190

Please use the following code example to get the desired output. You can insert the content according to your requirement into the document using the same approach as shared below. Please use DocumentBuilder.InsertHtml method to insert the HTML into document.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.getCurrentSection().getPageSetup().setDifferentFirstPageHeaderFooter(true);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.write("Note 1 continued");
builder.moveToSection(0);

builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2);
builder.writeln("Notes 1");
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.NORMAL);
                
for (int j = 0; j < 50; j++)
{
    builder.writeln("Some content");
}

builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);

builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.write("Note 2 continued");
builder.moveToSection(1);
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2);
builder.writeln("Notes 2");
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.NORMAL);

for (int j = 0; j < 50; j++)
{
    builder.writeln("Some content");
}

doc.save(MyDir + "output.docx");
1 Like

Thanks @tahir.manzoor,

Your code helped me well and it had resolved my issue.

Regards,
Ashish M

@amestry29190

Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.