Can I get some sample code on modifying the Header/Footer of word docs? I currently read in the document and use the DocumentBuilder to write(). This all works fine.
However, I will need to modify and/or replace the complete Header.
Can I get some sample code on modifying the Header/Footer of word docs? I currently read in the document and use the DocumentBuilder to write(). This all works fine.
However, I will need to modify and/or replace the complete Header.
Here is an example for header/footer creation:
DocumentBuilder builder = new DocumentBuilder(doc);
// Set to 'true' if you want different headers/footers for first, even and odd pages.
builder.getPageSetup().setDifferentFirstPageHeaderFooter(false);
builder.getPageSetup().setOddAndEvenPagesHeaderFooter(false);
builder.getPageSetup().setHeaderDistance(20);
builder.moveToHeaderFooter(HeaderFooterType.Header_Primary);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.Right);
builder.write("ASPOSE.WORDS DYNAMIC TEMPLATE");
builder.moveToHeaderFooter(HeaderFooterType.Footer_Primary);
// Insert table with invisible borders to align one part of the footer information to the left,
// and the other to the right.
builder.getRowFormat.setAllowAutoFit(true);
builder.startTable();
// Set font properties for the footer.
builder.getFont().setSize(12);
builder.getFont().setName("Arial");
// Insert 'Page X of Y' info, aligned to the left.
builder.insertCell();
builder.write("Page ");
builder.insertField("PAGE", "");
builder.write(" of ");
builder.insertField("NUMPAGES", "");
builder.getCellFormat().setWidth(WordConvert.pixelToPoint(500));
builder.getCurrentParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.Left);
// Insert copyright info, aligned to the right.
builder.insertCell();
builder.write("(C) 2005 Aspose Pty Ltd. All rights reserved.");
builder.getCellFormat().setWidth(WordConvert.pixelToPoint(1000));
builder.getCurrentParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.Right);
builder.endRow();
builder.endTable();
To clean up headers/footers in the section use
section.ClearHeadersFooters();
Another example shows how to replace text in headers/footers:
doc.getFirstSection().getHeadersFooters[HeaderFooterType.Footer_Primary].getRange().replace("(C) 2005 Aspose Pty Ltd.", "(C) 2006 Aspose Pty Ltd.", true, true);
To manipulate existing headers/footers content you need to use them same approach as with section content. Header/Footer contains treelike structure of nodes (paragraphs, tables, runs, etc.) You can get a hold of any of them through indexers and getChild methods, and then you can add/remove nodes or change the text of runs.