We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Finding content of the existing header and footer

Am evaluating Aspose for our Use Case and one of the very last things to validate is how to find if there is an existing header and footer value present in the document.

I have been create headers and footers but unable to find if they are existing already. Tried the following which does not give me what I expect

Blockquote
HeaderFooter header = new HeaderFooter(document, HeaderFooterType.HEADER_PRIMARY);
String originalHeaderText = header.getText();

@aspose1212

Following code example shows how to check the existing header of document and add new header if it does not exist.

Document document = new Document(MyDir + "in.docx");
HeaderFooter header = document.getSections().get(0).getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY);

if (header == null) {
    // There is no header of the specified type in the current section, create it.
    header = new HeaderFooter(document, HeaderFooterType.HEADER_PRIMARY);
    document.getSections().get(0).getHeadersFooters().add(header);
}

Many thanks!
So if I have to set the same primary header throughout the document (all sections) then do I need to go to each individual sections and set or would the following work?

Blockquote
DocumentBuilder builder = new DocumentBuilder(document);

    builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
    builder.write("document title");
    builder.getCurrentParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);

If so, how could I use DocumentBuilder to work with sections, for e.g. using the code you shared earlier?
I tried to move using DocumentBuilder to a particular section but that did not work

Blockquote

Document document = new Document(MyDir + “in.docx”);
HeaderFooter header = document.getSections().get(0).getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY);

@aspose1212

Yes, you need to iterate through each section and create the header/footer according to your requirement.

Please ZIP and attach your input and expected output documents here for testing. We will investigate the issue and provide you more information on it.