How to Merge Multiple Word Documents into One using C#

Hi,

My requirement is to get two or three Word documents from my file system and merge them to create one Word document.

I want the header and footer of the first Word DOC to be the header and footer for the merged DOC.

Regards
Shalini

Hi,

Well, I am not very sure about your requirement. But I think if you want to merge some Excel Workbook to make it one, you may try some APIs which Aspose.Cells provides for your task i.e., Workbook.Combine, Workbook.Copy or Worksheet.Copy etc.

Thank you.

I am sorry, I did not explain correctly.

I want to merge few Word documents together.

The header and footer of the merged DOC should be same as that of the first DOC selected

By DOC I mean a .doc file

I am not sure where to post a question related to Word documents

Regards
Shalini

Hi Shalini,

Well, you should post your query in Aspose.Words forum. I have moved your thread to Aspose.Words forum and one of our developer will help you soon.

Thank you.

Hi

Thanks for your inquiry. Please see the following link to learn how to merge Word documents.

I modified the code provided in this article so you can specify flag that determines whether headers and footers from destination Word document will be used. See the following C# code:

Code to Merge Multiple Word Documents into One using C#

public void Merge_Multiple_Word_Documents_Into_One_Using_CSharp()
{
    Document doc = new Document(@"Test161\merge word documents 1.doc");
    Document docx = new Document(@"Test161\merge word documents 2.docx");
    Aspose_Merge_Word_Documents(doc, docx, true);
    doc.Save(@"Test161\out.rtf");
}

/// A useful function to easily merge one Word document to another.
/// The destination Word document where to append to
/// The source Word document
/// Determines whether headers and footers of destination Word document are used
public void Aspose_Merge_Word_Documents(Document dstDoc, Document srcDocx, bool useDstHeaders)
{
    // Loop through all sections in the source word document.
    // Section nodes are immediate children of the Word Document node so we can just enumerate the Document.
    foreach (Section srcSection in srcDocx)
    {
        // Because we are copying a section from one word document to another,
        // it is required to import the Section node into the destination word 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 word document.
        Section dstSection = (Section)dstDoc.ImportNode(srcSection, true, ImportFormatMode.KeepSourceFormatting);
        if (useDstHeaders)
        {
            // Remove original headers and footers
            dstSection.HeadersFooters.Clear();

            // Link headers and footers to previouse section
            dstSection.HeadersFooters.LinkToPrevious(true);
        }

        // Now the new section node can be merged to the destination word document.
        dstDoc.AppendChild(dstSection);
    }
}

Hope this helps.
Best regards.