javafooterasposeaspose.words

Aspose Word custom Footer for each page


What would be the correct approach to set specific Footer/Header per page using Aspose.Word library (in Java)? Consider the Document which has different number of Sections and Pages.

I did some tries with the Document having 2 Pages in Section 1 and singe page for rest sections. As far as I understood the document should have equal number of sections and pages in order to set custom Footer per page.

I suppose the part of final solution is here Adding seperate header/footer in each page using DOM in aspose.words

However, this code does not work correct as all footer texts get be written on the single page's footer. Moreover the number of recognised pages is higher that actual is.

Update 2023-10-24

This is the method which should add custom footer, but nothing really is added to footer.

private void addCustomFooter() {
        var collector = new LayoutCollector(builder.getDocument());
        NodeCollection<Paragraph> paragraphs = builder.getDocument().getChildNodes(NodeType.PARAGRAPH, true);
        log.info("Paragraphs total count={}", paragraphs.getCount());
        int pageIndex = 1;
        for (final Paragraph para : paragraphs) {
            if (collector.getStartPageIndex(para) == pageIndex) {
                log.info("Paragraph, pageIndex={}", pageIndex);
                builder.moveTo(para.appendChild(new Run(builder.getDocument())));
                builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);
                pageIndex++;
            }
        }
        pageIndex = 1;
        
        for (final Section section : builder.getDocument().getSections()) {
            log.info("Section. pageIndex={}", pageIndex);
            var headerFooterType = HeaderFooterType.FOOTER_PRIMARY;
            HeaderFooter footer = section.getHeadersFooters().getByHeaderFooterType(headerFooterType);
            if (footer == null) {
                footer = new HeaderFooter(builder.getDocument(), headerFooterType);
                section.getHeadersFooters().add(footer);
            }
            builder.moveToSection(builder.getDocument().getSections().indexOf(section));
            builder.moveToHeaderFooter(headerFooterType);
            builder.write("Page: " + pageIndex);
            pageIndex++;
        }
    }

Solution

  • Yes, you are right, in MS Word document headers/footers are defined per section. Each section in MS Word document can have 3 types of header and footer - First, Primary and Even pages. In your case it is required to split document by pages, you can achieve this using Document.ExtractPages. For example see the following code:

    Document doc = new Document("C:\\Temp\\in.docx");
    
    // Clone source document.
    Document result = (Document)doc.deepClone(false);
    
    // Split and rejoin document by pages.
    for (int i = 0; i < doc.getPageCount(); i++)
    {
        Document page = doc.extractPages(i, 1);
        // Remove old Header/Footer
        for (Section s : page.getSections())
        {
            s.getHeadersFooters().clear();
            s.getPageSetup().setOddAndEvenPagesHeaderFooter(false);
            s.getPageSetup().setDifferentFirstPageHeaderFooter(false);
        }
        // Add new header to the first section of page.
        DocumentBuilder pageBuilder = new DocumentBuilder(page);
        pageBuilder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
        pageBuilder.write("This is specific header for " + i + " page");
    
        result.appendDocument(page, ImportFormatMode.USE_DESTINATION_STYLES);
    }
    
    result.save("C:\\Temp\\out.docx");