Merging heades

Hi,
i am having two documetns with their own headrs and footers i.e document1, document 2.
i want the document 3 having the body of document2 and headers and footers of dcoument 3 should mege the headers and footers of document1 and document2
please find tha attached documents.
i need the out as doc3 and inputs are doc1,doc2.

Hi Sreelakshmi,

Thanks for your inquiry. Please use the following code snippet to merge the header/footer of documents. The MergeHeaderFooter method gets the contents of header/footer of first section of source document and append into the output document’s header/footer. Hope this helps you.

Please let us know if you have any more queries.

Document doc1 = new Document(MyDir + "doc1.docx");

Document doc2 = new Document(MyDir + "doc2.docx");
Document doc3 = new Document();
MergeHeaderFooter(doc1, doc3, HeaderFooterType.HEADER_PRIMARY);
MergeHeaderFooter(doc1, doc3, HeaderFooterType.FOOTER_PRIMARY);
MergeHeaderFooter(doc2, doc3, HeaderFooterType.HEADER_PRIMARY);
MergeHeaderFooter(doc2, doc3, HeaderFooterType.FOOTER_PRIMARY);
doc3.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
Using the above code i have created the document with the merged headers and footers and i have appended the new document say doc4 to the doc3 and saved the doc3.
when i try to open the saved doc3 i am getting the error.
Please reply me as early as possible.
Please find ate attached error screenshot

Hi Sreelakshmi,

Thanks for your inquiry.

I have tested the scenario while using latest version of Aspose.Words for Java with your shared documents and have not found the shared issue. Please use the latest version of Aspose.Words for Java. I have attached the output document with this post. If you are using some different documents, please share those documents here for testing. I will investigate the issue on my side and provide you more information.

I have added the below code to add the body content to the doc3.
i am using latest asposejar.

Document doc1 = new Document(MyDir + "doc1.docx");
Document doc2 = new Document(MyDir + "doc2.docx");
Document doc3 = new Document();
/// Added code
Document doc4 = new Document();
doc4.removeAllChiuldern();
DocumentBuilder doc3builder = new DocumentBuilder(doc3);
NodeImporter imp = new NodeImporter(doc2, doc3, ImportFormatMode.KEEP_SOURCE_FORMATTING);
ArrayList doc3array = new ArrayList();
for (int i = 0; i <doc2.getSections().getCount(); i++)
{
    doc3.getSections().get(i).getBody.removeAllChildern();
    doc3array = extractContent(doc2.getSections().get(i).getBody().getFirstParagraph(), doc2.getSections().get(i).getLastParagraph(), true);
    for (Node node: (Iterable) doc3array)
    {
        Node impNode = imp.importNode(nde, true);
        doc3.getSections().get(i).getBody().appendChild(impNode);
    }
    doc3builder.moveToDocumentEnd();
    Doc3builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
}
MergeHeaderFooter(doc1, doc3, HeaderFooterType.HEADER_PRIMARY);
MergeHeaderFooter(doc1, doc3, HeaderFooterType.FOOTER_PRIMARY);
MergeHeaderFooter(doc2, doc3, HeaderFooterType.HEADER_PRIMARY);
MergeHeaderFooter(doc2, doc3, HeaderFooterType.FOOTER_PRIMARY);
doc4.appendDocument(doc1, ImportFormatMode.KEEP_SOURCE_FORMAT);
doc4.appendDocument(doc3, ImportFormatMode.KEEP_SOURCE_FORMAT);
doc4.appendDocument(doc1.ImportFormatMode.KEEP_SOURCE_FORMAT);
doc4.save(output.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);
        }
    }
}

If opened the template.docx file i am getting the reported error.
Please let me know how to solve the above issue as early as possible.
Sreelakshmi

Hi Sreelakshmi,

Thanks for sharing the details. I have managed to reproduce the same issue at my side. We had already logged this issue as WORDSNET-7635 in our issue tracking system.

The good news is, this issue has now been resolved and its fix will be included in the next version of Aspose.Words (v13.1.0) which is planned to be released by the end of this month (around January’ 31, 2013). We will inform you via this forum thread as soon as the new release is published at the end of the month.

The workaround of this issue is, please save your final document to Doc file format first and then save back to Docx. This will resolve your issue.

We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-7635) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(4)