Merge documents that have header and footer

I have two documents. Each has header and footer. I need to merge these two documents and make one with a page break. I need to have same header and footer just like in original documents.
Please help me.

Hi
Thanks for your request. I think you should link header to previous section. For example see the following code:

// Open first document (with header)
Document dst = new Document(@"Test207\dst.doc");
// Open second document (with footer)
Document src = new Document(@"Test207\src.doc");
// Set link to previouse 
src.FirstSection.HeadersFooters.LinkToPrevious(HeaderFooterType.HeaderPrimary, true);
// Merge documents
dst.AppendDocument(src, ImportFormatMode.KeepSourceFormatting);
// Save output document
dst.Save(@"Test207\out.doc");

Hope this could help you.
Best regards.

I have this code as below. Issues are:

  1. The second document does not go to the new page
  2. src and dst documents have different header and footer. When merged it keeps the header and footer of dst document. Header and footer of src doc is lost.
    Please help. I am still using evaluation version. Going to buy soon.
src.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
src.FirstSection.HeadersFooters.LinkToPrevious(Aspose.Words.HeaderFooterType.HeaderPrimary, true);
src.FirstSection.HeadersFooters.LinkToPrevious(Aspose.Words.HeaderFooterType.FooterPrimary, true);
foreach (Aspose.Words.Section srcSection in src)
{
    Node dstSection = dst.ImportNode(srcSection, true, 
    ImportFormatMode.KeepSourceFormatting);
    dst.AppendChild(dstSection);
}

Hi
Thanks for your request. Please try using the following code:

// Open master document
Document master = new Document(@"Test002\dst.doc");
// Open sub document
Document srcdoc = new Document(@"Test002\src.doc");
// Set Section start of first section of source document
srcdoc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
srcdoc.FirstSection.HeadersFooters.LinkToPrevious(false);
// Append source document to master
master.AppendDocument(srcdoc, ImportFormatMode.KeepSourceFormatting);
// Save master document
master.Save(@"Test002\out.doc");

Hope this helps.
Also please attach your documents for testing.
Best regards.

where does the highlighted lines go in my code above? Does it go inside the for loop?
AppendDocument does not exist. How can I use AppendDocument function? I am using node section. Isn’t that correct?
Please help.
caci

Hi
Thanks for your request. I meant that you should use my code instead yours. You can also put highlighted code before Sections loop.
Best regards.

where is AppendDocument function?

Hi
Thanks for your request. Document.AppendDocument See the following link:
https://reference.aspose.com/words/net/aspose.words/document/appenddocument/
Which version of Aspose.Words are you using? If you use old version then see the following link.
https://docs.aspose.com/words/net/insert-and-append-documents/https://reference.aspose.com/words/net/aspose.words/document/appenddocument/
Best regards.

Hi,
You have been so helpful in this forum.
I have attached two documents that need to be merged. Each doc has its own header and footer.
Please give me some code that merge these documents and make one. After merging, each doc’s header and footer should be maintained.
Thank you.

Hi
Thank you for additional information. Code is the same as I provided in my previous post.

// Open master document
Document master = new Document("dstDoc.doc");
// Open sub document
Document srcdoc = new Document("srcDoc.doc");
// Set Section start of first section of source document
srcdoc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
srcdoc.FirstSection.HeadersFooters.LinkToPrevious(false);
// Append source document to master
master.AppendDocument(srcdoc, ImportFormatMode.KeepSourceFormatting);
// Save master document
master.Save("out.doc");

But if you use old version of Aspose.Words and there is no AppenDocument method then you should use the following code:

public void Test002()
{
    // Open master document
    Document master = new Document(@"Test002\dstDoc.doc");
    // Open sub document
    Document srcdoc = new Document(@"Test002\srcDoc.doc");
    // Set Section start of first section of source document
    srcdoc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
    srcdoc.FirstSection.HeadersFooters.LinkToPrevious(false);
    // Append source document to master
    AppendDoc(master, srcdoc);
    // Save master document
    master.Save(@"Test002\out.doc");
}
/// 
/// A useful function that you can use to easily append one document to another.
/// 
/// The destination document where to append to.
/// The source document.
public void AppendDoc(Document dstDoc, Document srcDoc)
{
    // Loop through all sections in the source document. 
    // Section nodes are immediate children of the Document node so we can just enumerate the Document.
    foreach (Section srcSection in srcDoc)
    {
        // Because we are copying a section from one document to another, 
        // it is required to import the Section node into the destination document.
        // This adjusts any document-specific references to styles, lists, etc.
        //
        // Importing a node creates a copy of the original node, but the copy
        // is ready to be inserted into the destination document.
        Node dstSection = dstDoc.ImportNode(srcSection, true, ImportFormatMode.KeepSourceFormatting);
        // Now the new section node can be appended to the destination document.
        dstDoc.AppendChild(dstSection);
    }
}

Hope this helps.
Best regards.

Hi,

I am currently evaluating Aspose for possible use within our company for the purpose of merging word documents into a single PDF programatically. I’d like to say that I was very appreciative when I came across this post as I was having trouble with headers, footers and page numbering. The page numbering was solved with the following code added just prior to appending the document:

srcdoc.FirstSection.PageSetup.RestartPageNumbering = true;

Our aim is to merge multiple word documents into one so that the user can print them all together in one go but all their source formatting should be maintained. Everything is looking OK for now but do you know of any formatting that may be missed by the code above? Another option would be to export the documents to PDF and then merge them using Aspose.Pdf.Kit but we’d prefer to stick to a single library.

Thanks for any response,
Kieron.

P.S. I think this example would be very useful incorporated in an example on the main site.

Hi

Thanks for your inquiry. I think, you can find a solution of this issue here:
https://forum.aspose.com/t/78349
Hope this helps. Please let me know if you need more assistance, I will be glad to help you.
Best regards,