Hi Kunal,
Thanks for your inquiry. You can fix the output of “Generated Word Doc with Footers.docx” document by using the following code (see attached output):
<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>
<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>Document doc = <span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 0, 128); font-weight: bold;”>new <span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>Document(<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 128, 0); font-weight: bold;”>“D:<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 0, 128); font-weight: bold;”>\<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 128, 0); font-weight: bold;”>temp<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 0, 128); font-weight: bold;”>\<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 128, 0); font-weight: bold;”>Generated Word Doc with Footers.docx”<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>);
<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>DocumentBuilder builder = new DocumentBuilder(doc);
for(Section sec : doc.getSections()){
sec.clearHeadersFooters();
}
insertFooterForDocument(builder);
doc.save(“D:\temp\awjava-17.2.0.docx”);
<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>private static void insertFooterForDocument(DocumentBuilder builder){
Section currentSection = builder.getDocument().getFirstSection();
PageSetup pageSetup = currentSection.getPageSetup();
// Specify if we want headers/footers of the first page to be different from other pages.
// You can also use PageSetup.OddAndEvenPagesHeaderFooter property to specify
// different headers/footers for odd and even pages.
pageSetup.setDifferentFirstPageHeaderFooter(true);
// — Create footer for pages other than first. —
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
insertHFContent(builder);
// — Create footer for first page. —
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_FIRST);
insertHFContent(builder);
builder.moveToDocumentEnd();
}
private static void insertHFContent(DocumentBuilder builder){
// We use table with two cells to make one part of the text on the line (with page numbering)
// to be aligned left, and the other part of the text (with copyright) to be aligned right.
builder.startTable();
// Clear table borders
builder.getCellFormat().clearFormatting();
builder.insertCell();
// Set first cell to 1/3 of the page width.
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(100 / 3));
// Insert page numbering text here.
// It uses PAGE and NUMPAGES fields to auto calculate current page number and total number of pages.
builder.write("Page “);
builder.insertField(“PAGE”, “”);
builder.write(” of ");
builder.insertField(“NUMPAGES”, “”);
// Align this text to the left.
builder.getCurrentParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
builder.insertCell();
// Set the second cell to 2/3 of the page width.
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(100 * 2 / 3));
builder.write(“ABC Classified”);
// Align this text to the right.
builder.getCurrentParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
builder.endRow();
builder.endTable();
}
Hope, this helps.
Best regards,