Unique headers and footers per page

Hiyas

I try to create unique headers and footers per page using Aspose.Words for Java. I had a look at the samples, but for some reasons, I don’t seem to be able to create such a document myself. I stripped everything down to this:

Document document = new Document();
document.removeAllChildren();
document.ensureMinimum();
DocumentBuilder documentBuilder = new DocumentBuilder(document);
documentBuilder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
for (int page = 0; page < 5; page++)
{
    documentBuilder.getCurrentSection().getHeadersFooters().linkToPrevious(false);
    documentBuilder.writeln("Content of page " + (page + 1)); documentBuilder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
    documentBuilder.write("Header of page " + (page + 1)); documentBuilder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
    documentBuilder.write("Footer of page " + (page + 1));
    documentBuilder.moveToSection(0);
    documentBuilder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
}
document.save("c:\test.doc");

but for some reason, this does create a document and it does have some unique headers and footers, but not all of them and it places all the content to the end of the document. I’m sure I just forgot to set something but I can’t find out what it is. Can anyone help me out here?

Best regards and thanks
Rog

Hi
Thanks for your interest in Aspose.Words. Please try using the following code snippet.

Document document = new Document();
document.removeAllChildren();
document.ensureMinimum();
DocumentBuilder documentBuilder = new DocumentBuilder(document);
documentBuilder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
for (int page = 0; page < 5; page++)
{
    documentBuilder.getCurrentSection().getHeadersFooters().linkToPrevious(false);
    documentBuilder.writeln("Content of page " + (page + 1));
    documentBuilder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
    documentBuilder.write("Header of page " + (page + 1));
    documentBuilder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
    documentBuilder.write("Footer of page " + (page + 1));
    // Move cursor to document end
    documentBuilder.moveToDocumentEnd();
    // Check if this is last page
    if (page != 4)
        documentBuilder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
}
document.save("test.doc");

But note Aspose.Words doesn’t have the possibility to know the end of a page. Aspose.Words document represents content and formatting of a document, not its layout into lines and pages. This feature is called pagination and it is not released yet. Please see FAQ for more information.
Best regards.

THanks… I tried that out, but this creates a document where the first two pages are empty and use the header and footers of the first page and the other following pages are reversed (page three is page five, then page four stays page for, then comes page 3 and as last page, there’s another page inserted that contains the headers and footers for page 2 but the cotent of both page 1 and two (see attachment).

What do you mean with pagination? What I’m trying to do is not letting Aspose fnd out where the page breaks need to be, but actually inserting my own page breaks. If the content that I insert on a page does not fit the page, its perfectly legal for Word to put a paragraph on the next page, I don’t have a problem with that. But I need to make sure that I can insert my own page breaks where needed as I can using Word as well.

Did I forget something here maybe?

Best regards and thanks
Roger

Hi
Please see the attached document. This is output document I got using this code. Could you please create document your need using MS word and attach it here? I will investigate the problem and try to help you.
Best regards.

Hiyas

Hey, you’re right. I’m sorry, first I attached the wrong document and then I saw that the new test file wasn’t compiled properly. This works very well. So it is correct that, when I need to switch from Headers/Footers to the content part again, that I can use moveToDocumentEnd() to achieve that or should I store a current position and return to that (and if yes, how?)

Best regardsa and many thanks!
Rog

Hi
I think that you can use moveToDocumentEnd(). But if you need you can store current position of the DocumentBuilder cursor. For example:

// Store position before moving to header or footer
Node currentNode = documentBuilder.getCurrentParagraph();
// ..............................................
// restore position before inserting SectionBreak
documentBuilder.moveTo(currentNode);

Best regards,