Copy Header Footer from template to another document

How can I Copy Header Footer from template to another document?

Hi there,

Thanks for your inquiry.
Please use following code example to copy Header/Footer from one document into another. Hope this helps you.
Please let us know if you have any more queries.

Document dest = new Document(MyDir + "Destination.docx");
Document src = new Document(MyDir + "source.docx");
MergeHeaderFooter(src, dest, HeaderFooterType.HEADER_PRIMARY);
dest.save(MyDir + "Out.docx");
public static void MergeHeaderFooter(Document srcDoc, Document dstDoc, int headerType) throws Exception
{
    for (Section section : dstDoc.getSections())
    {
        HeaderFooter header = section.getHeadersFooters().getByHeaderFooterType(headerType);
        if (header == null)
        {
            // There is no header of the specified type in the current section, create it.
            header = new HeaderFooter(section.getDocument(), headerType);
            section.getHeadersFooters().add(header);
        }
        for (Node srcNode : (Iterable)srcDoc.getFirstSection().getHeadersFooters().getByHeaderFooterType(headerType).getChildNodes())
        {
            Node dstNode = dstDoc.importNode(srcNode, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
            header.appendChild(dstNode);
        }
    }
}

Hi,
Thanks for your response. This code is not working for me. Please find attached the Template.docx from which the header and footer needs to be copied and the Input.docx to which it is to be copied. Also find attached the expected output.docx.

Also,I want the first page of Template.docx to be appended at the starting of the Input.docx.

Thanks.

Hi there,

Thanks for your inquiry. Following code example import header/footer from template document into input document and append the template document at the start of final output document. I have attached the output document with this post for your kind reference.

public static void MergeHeaderFooter(Document srcDoc, Document dstDoc, int headerType) throws Exception
{
    for (Section section : dstDoc.getSections())
    {
        HeaderFooter header = section.getHeadersFooters().getByHeaderFooterType(headerType);
        if (header == null)
        {
            // There is no header of the specified type in the current section, create it.
            header = new HeaderFooter(section.getDocument(), headerType);
            section.getHeadersFooters().add(header);
        }
        for (Node srcNode : (Iterable)srcDoc.getFirstSection().getHeadersFooters().getByHeaderFooterType(headerType).getChildNodes())
        {
            Node dstNode = dstDoc.importNode(srcNode, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
            header.appendChild(dstNode);
        }
    }
}
Document input = new Document(MyDir + "Input.docx");
Document template = new Document(MyDir + "Template.docx");
// Add header/footer from template into input document
MergeHeaderFooter(template, input, HeaderFooterType.HEADER_PRIMARY);
MergeHeaderFooter(template, input, HeaderFooterType.FOOTER_PRIMARY);
// Add template document at the start of final document
template.appendDocument(input, ImportFormatMode.KEEP_SOURCE_FORMATTING);
template.save(MyDir + "Out.docx");

If you want to extract the first page of template document, please use PageSplitter utility to extract the first page of template document and use Document.appendDocument method to add the extracted document at the start of final document.

Please let us know if you have any more queries.