DEV Community

jelizaveta
jelizaveta

Posted on

Merge Word Documents in C# (No More Manual Copy-Paste)

Many projects need to combine multiple Word documents into a single finished file—for example, combining materials from different chapters into one report, or merging submissions from various sources for delivery. At the same time, questions like whether the formatting stays correct after merging, whether pagination matches expectations, and whether the code is clean enough often determine whether the solution can be implemented smoothly.

Below are two common merging strategies, both implemented using Spire.Doc for .NET:

  • Method 1: Append the entire second document to the end of the first (usually creates a "start on a new page" style result)
  • Method 2: Iterate through the second document's Sections, clone their content objects, and append them to the last Section of the first (ideal for more fine-grained structure control)

Spire.Doc: Purpose & Installation

Spire.Doc is a .NET component library for reading, editing, and generating Word documents (DOC/DOCX). It provides a structure-oriented API—for example, loading documents, inserting content, traversing Section/Body/object collections, copying document elements, and saving as DOCX. Compared with manually parsing the DOCX zip package and XML, using Spire.Doc can significantly reduce development effort.

Installation (recommended via NuGet):

  1. Open your project in Visual Studio
  2. Right-click the project → Manage NuGet Packages
  3. Search for and install Spire.Doc for .NET
  4. Add the namespace reference in your code: using Spire.Doc;

After installation, you can load and manipulate Word files directly through the Document class.

Method 1: Append Entire Document (New Page Effect)

When your goal is to treat Doc2 as a whole and attach it to Doc1 afterward, and you want the merged result to feel close to Word's intuitive "Insert Document / Append Content" experience, you can use InsertTextFromFile.

Approach:

  • Create a Document object to hold the merged document
  • Load the main document (Doc1)
  • Insert another Word document entirely into the main document
  • Save the result as a new merged file

Sample code:

using Spire.Doc;

namespace MergeWord
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a Document instance
            Document document = new Document();

            // Load the original Word document
            document.LoadFromFile("Doc1.docx", FileFormat.Docx);

            // Insert another Word document entirely to the original document
            document.InsertTextFromFile("Doc2.docx", FileFormat.Docx);

            // Save the result document
            document.SaveToFile("MergedWord.docx", FileFormat.Docx);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

When to Use Method 1

  • Less code, faster development
  • Best for "overall append" merges by document boundaries
  • Less control at the Section level, but usually sufficient for common consolidation needs

Method 2: Append to Last Section (Precise Control)

If the merge requires more precision—e.g., keeping content continuous within the same Section as much as possible, or appending the second document's objects one by one to the end of the first—you can use the second approach: iterate through doc2.Sections, clone each section's Body.ChildObjects, and add them to doc1.LastSection.Body.ChildObjects.

Approach:

  • Load doc1 and doc2
  • Iterate through doc2.Sections
  • For each section, iterate through section.Body.ChildObjects
  • Add cloned objects to doc1.LastSection.Body.ChildObjects
  • Save doc1 as the merged result

Sample code:

using Spire.Doc;

namespace MergeWord
{
    class Program
    {
        static void Main(string[] args)
        {
            // Load two Word documents
            Document doc1 = new Document("Doc1.docx");
            Document doc2 = new Document("Doc2.docx");

            // Loop through the second document to get all the sections
            foreach (Section section in doc2.Sections)
            {
                // Loop through child objects in the section body
                foreach (DocumentObject obj in section.Body.ChildObjects)
                {
                    // Get the last section of the first document
                    Section lastSection = doc1.LastSection;

                    // Add cloned objects to the last section of the first document
                    lastSection.Body.ChildObjects.Add(obj.Clone());
                }
            }

            // Save the result document
            doc1.SaveToFile("MergeDocuments.docx", FileFormat.Docx);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

When to Use Method 2

  • Closest to "concatenate content objects directly into the last section"
  • Uses Clone() to copy objects safely and avoid direct-reference issues
  • Better suited for complex cases (multi-section documents, header/footer, pagination rules)—final results still depend on your templates

Method 1 vs. Method 2: How to Choose

  • If you want a quick merge and an intuitive "append starting on a new page" effect: Method 1 is easier.
  • If you need stronger structure control and want to append doc2 content into the last Section of doc1: Method 2 is a better match.
  • If your templates include headers/footers, different page orientations (landscape/portrait), or complex section configurations: It's recommended to test with Method 2 first.
  • For ordinary text and table merges: Method 1 is typically faster and more reliable.

Top comments (0)