Header Footer Replacement using Aspose for Java

Hi
I have a requirement to replace Header & Footer of a Document with the Header & Footer of another document. ie from a source doc to a target source. I tried the same but am getting the below error.
Exception in thread “main” java.lang.IllegalArgumentException: The newChild was created from a different document than the one that created this node.
at com.aspose.words.CompositeNode.zzZ(Unknown Source)
Can you please help me on this requirement.
Please find the program below which i used for replacing header and footer.

public class Test44 {
/**
* The main entry point for the application.
*/
public static void main(String[] args) throws Exception
{
    // The path to the documents directory.
    String dataDir = "D:/Test2/";

    String fileName = "Test4.doc";
    // Load the template document.
    Document doc = new Document(dataDir + fileName);

    Document doc2 = new Document(dataDir + "Test5.doc");

    // Create an instance of sender class to set it's properties.
    Sender sender = new Sender();
    sender.setName("LINQ Reporting Engine");
    sender.setMessage("Hello World");

    // Create a Reporting Engine.
    ReportingEngine engine = new ReportingEngine();

    PdfSaveOptions options = new PdfSaveOptions();
    options.getOutlineOptions().setDefaultBookmarksOutlineLevel(1);
    options.getOutlineOptions().setDefaultBookmarksOutlineLevel(2);

    DataSet ds = new DataSet();
    ds.readXml(dataDir+"AcutalData4.xml");

    System.out.println("ds = "+ds);
// ds.readXml("aaa".get)
    // Execute the build report.
    engine.buildReport(doc, ds);

    dataDir = dataDir + "AcutalData_otput5.pdf";

    HeaderFooter f1 = null;
    HeaderFooter f2 = null;
    HeaderFooter f3 = null;

    System.out.println("Sections " + doc2.getSections().getCount());

    for (Section section : doc2.getSections()) {
    	// Up to three different footers are possible in a section (for first, even and odd pages).
    	// We check and delete all of them.

    	f1 = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_FIRST);

    	// Primary footer is the footer used for odd pages.
    	f2 = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);

    	f3 = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_EVEN);

    }

    /*
    for (Section section : doc.getSections()) {
    	// Up to three different footers are possible in a section (for first, even and odd pages).
    	// We check and delete all of them.
    	HeaderFooter footer;

    	footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_FIRST);
    	if (footer != null)
    		footer.remove();

    	// Primary footer is the footer used for odd pages.
    	footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);
    	if (footer != null)
    		footer.remove();

    	footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_EVEN);
    	if (footer != null)
    		footer.remove();

    }
    */

	DocumentBuilder builder = new DocumentBuilder(doc);

	Section currentSection = builder.getCurrentSection();
	builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
	currentSection.getHeadersFooters().add(f1.deepClone(true));

    for (Section section : doc.getSections()) {
    	  section.getHeadersFooters().clear();
    	// Up to three different footers are possible in a section (for first, even and odd pages).
    	// We check and delete all of them.
    	HeaderFooter footer;

    	footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_FIRST);
// if (footer != null)
section.getHeadersFooters().add(f1.deepClone(true));
    	// Primary footer is the footer used for odd pages.
    	footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);
    	if (footer != null)
    		 section.getHeadersFooters().add(f2.deepClone(true));

    	footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_EVEN);
// if (footer != null)
section.getHeadersFooters().add(f3.deepClone(true));
    }

    doc.save(dataDir);

    System.out.println("\nTemplate document is populated with the data about the sender.\nFile saved at " + dataDir);

}
}

@jjebin,

Thanks for your inquiry. HeaderFooter is a section-level node and can only be a child of Section. There can only be one HeaderFooter or each HeaderFooterType in a Section. Following code example shows how to copy headers and footers of a section into another section. Hope this helps you.

Document srcDoc = new Document(MyDir + "Doc1.docx");
Document dstDoc = new Document(MyDir + "Doc2.docx");
CopyHeaderFooter(srcDoc.getFirstSection(), dstDoc.getFirstSection());
dstDoc.save(MyDir + "output.docx");

public static void CopyHeaderFooter(Section srcSection, Section dstSection) throws Exception
{
    //Remove existing header/footer
    dstSection.getHeadersFooters().clear();

    for (HeaderFooter headersFooter : srcSection.getHeadersFooters())
    {
        // There is no header of the specified type in the current section, create it.
        HeaderFooter header = new HeaderFooter(dstSection.getDocument(), headersFooter.getHeaderFooterType());
        dstSection.getHeadersFooters().add(header);

        //Copy nodes from source document to destination
        for (Node srcNode : (Iterable<Node>)headersFooter.getChildNodes())
        {
            Node dstNode = dstSection.getDocument().importNode(srcNode, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
            header.appendChild(dstNode);
        }
    }
}