Apply Header Footer from different Docx File

Hi Support Team,
I am struggling with setting header and footer from different header.docx file and footer.docx file
Our Ultimate goal is to set header in Output.docx from header.docx file and footer from footer.docx file

Please check below code snippet and let me know what am i doing wrong in it

Document headerDocument = new Document(testEntity.getExportSetting().getHeaderFile().getInputStream());

ps.setDifferentFirstPageHeaderFooter(false);
ps.setHeaderDistance(15);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.getCurrentSection().clearHeadersFooters();
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.insertDocument(headerDocument,ImportFormatMode.KEEP_SOURCE_FORMATTING);
builder.moveToDocumentStart();

Document footerDocument = new Document(testEntity.getExportSetting().getFooterFile().getInputStream());

ps.setDifferentFirstPageHeaderFooter(false);
ps.setFooterDistance(10);
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.getCurrentSection().clearHeadersFooters();
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.insertDocument(footerDocument,ImportFormatMode.KEEP_SOURCE_FORMATTING);
builder.moveToDocumentStart();

FOOTER.docx (13.2 KB)

HEADER.docx (38.3 KB)

@praful.meshram You can copy headers/footers from one document to another. You should simply import the appropriate headers/footers node. For example see the following code:

// The document that contains Header definitions
Document header = new Document("C:\\Temp\\header.docx");
// The document that contains footer definitions
Document footer = new Document("C:\\Temp\\footer.docx");
// The target document
Document mainBody = new Document("C:\\Temp\\dst.docx");

for (Section s : mainBody.getSections())
{
    copyHeaderFooter(s, header.getFirstSection(), HeaderFooterType.HEADER_PRIMARY);
    copyHeaderFooter(s, footer.getFirstSection(), HeaderFooterType.FOOTER_PRIMARY);
}

mainBody.save("C:\\Temp\\out.docx");
private static void copyHeaderFooter(Section dstSection, Section srcSection, 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;

    // 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();

    NodeImporter importer = new NodeImporter(srcSection.getDocument(), dstSection.getDocument(), ImportFormatMode.KEEP_SOURCE_FORMATTING);

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

The following articles might be useful for you:
https://docs.aspose.com/words/java/aspose-words-document-object-model/
https://docs.aspose.com/words/java/working-with-headers-and-footers/