Copy page number from two sections

Hello,
I’m having a problem and I can’t find the solution. I’m concatenating to word files. I would like to have the same footer page numbering in every sections. But it doesn’t work. In the end, the second section has no page numbers. Could you please help me to copy the footnote paragraph in the second section ?
The method I’m trying to make work is “SimpleTest4Aspose.appendPageNumbers” (the zip is attached to the topic)

Thanks for your help

test-classes.zip (308.2 KB)

@rodrigue.leopold Please try using the following code to copy header/footer from one section to another:

Document doc1 = new Document("C:\\Temp\\ecahier_Page_Garde.docx");
Document doc2 = new Document("C:\\Temp\\ecahier_Auteur.docm");

// Merge documents.
doc1.appendDocument(doc2, ImportFormatMode.KEEP_DIFFERENT_STYLES);

// Copy footer from the first section to the last section.
copyHeaderFooter(doc1.getFirstSection(), doc1.getLastSection(), HeaderFooterType.FOOTER_PRIMARY);

doc1.save("C:\\Temp\\out.docx");
private static void copyHeaderFooter(Section srcSection, Section dstSection, int hfType) {
    // Specify options to display different header/footer for first and odd/even pages.
    if (hfType == HeaderFooterType.HEADER_FIRST || hfType == HeaderFooterType.FOOTER_FIRST)
        dstSection.getPageSetup().setDifferentFirstPageHeaderFooter(srcSection.getPageSetup().getDifferentFirstPageHeaderFooter());

    if (hfType == HeaderFooterType.HEADER_EVEN || hfType == HeaderFooterType.HEADER_EVEN)
        dstSection.getPageSetup().setOddAndEvenPagesHeaderFooter(srcSection.getPageSetup().getOddAndEvenPagesHeaderFooter());

    // Get header/footer of the specified type from the source section.
    HeaderFooter srchf = srcSection.getHeadersFooters().getByHeaderFooterType(hfType);

    // Nothing to copy.
    if (srchf == null)
        return;

    System.out.println(srchf.getText().trim());

    // Get header/footer of the specified type from the destination section.
    HeaderFooter dsthf = dstSection.getHeadersFooters().getByHeaderFooterType(hfType);

    // Remove existing header/footer
    if (dsthf != null)
        dsthf.remove();

    // Copy whole header/footer into the destination document.
    dstSection.getHeadersFooters().add(srchf.deepClone(true));
}

After copying the footer looks like this:

Thanks, you’re the boss… Your code works perfectly. I didn’t know it was possible to deepClone the entire HeaderFooter Object.

PERFECT, it works… Thanks

1 Like

One last thing… Sorry it was not in the first message… Could you please tell me why the page “4” has no number… It must be because the first page is ignored, or something.
What option sould I call, in your first algorithm in order to have a page number on the fourth page ? (the doc is attached to the answer)TACZ-0-DTA 9_19-1061_V3 - candidat.zip (911.5 KB)

@rodrigue.leopold This occurs because the section has different first page header/footer option enabled and the first page footer is inherited from the first section. You should simply disable this option for the last section in your document:

sec.getPageSetup().setDifferentFirstPageHeaderFooter(false);

You’re really the boss… Thanks :slight_smile:

1 Like