Join or Combine Word files in C# and VB.NET

With GemBox.Document you can load multiple documents of any supported input format (DOCX, DOC, RTF, HTML, etc.), join them together, and save them as a single document of any supported output format (DOCX, RTF, HTML, PDF, etc.). For a list of supported input and output file formats, you can refer to the File format support help page.

If you're working with input PDF files, we recommend an alternative approach for merging PDF files using GemBox.Pdf.

To combine Word files into a single file, you need to merge the elements from each source document to the destination document. You cannot directly move or insert an element object from one document into another. Instead, you first need to import it to the destination document and then insert the imported element.

The following example shows the easiest way to combine multiple Word documents into one large document using C# and VB.NET code.

Combining Word files into one by importing content from source documents to destination document
Screenshot of multiple Word files joined into a single file
using GemBox.Document;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Word files that will be combined into one file.
        string[] files = 
        {
            "%#MergeFile01.docx%",
            "%#MergeFile02.docx%",
            "%#MergeFile03.docx%"
        };

        // Create destination document.
        var destination = new DocumentModel();

        // Merge multiple source documents by importing their content at the end.
        foreach (var file in files)
        {
            var source = DocumentModel.Load(file);
            destination.Content.End.InsertRange(source.Content);
        }

        // Save joined documents into one file.
        destination.Save("Merged Files.%OutputFileType%");
    }
}
Imports GemBox.Document

Module Program

    Sub Main()

        ' If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        ' Word files that will be combined into one file.
        Dim files As String() =
        {
            "%#MergeFile01.docx%",
            "%#MergeFile02.docx%",
            "%#MergeFile03.docx%"
        }

        ' Create destination document.
        Dim destination As New DocumentModel()

        ' Merge multiple source documents by importing their content at the end.
        For Each file In files
            Dim source = DocumentModel.Load(file)
            destination.Content.End.InsertRange(source.Content)
        Next

        ' Save joined documents into one file.
        destination.Save("Merged Files.%OutputFileType%")

    End Sub
End Module

Join or Combine Word sections

In case you don't want to have merged documents separated with a page break, you can individually import the Section elements of source documents and change the PageSetup.SectionStart of the first one to SectionStart.Continuous.

Or you could import the Block elements from each Section.Blocks collection and, as a result, create a merged document with just a single Section.

The following example shows how you can import sections from one document into another using C# and VB.NET code.

Combining Word files into one by importing source sections, without page break, to destination document
Screenshot of multiple Word sections joined into a single file
using GemBox.Document;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Word files that will be combined into one file.
        string[] files = 
        {
            "%#MergeFile01.docx%",
            "%#MergeFile02.docx%",
            "%#MergeFile03.docx%"
        };

        var destination = new DocumentModel();
        var firstSourceDocument = true;

        foreach (var file in files)
        {
            var source = DocumentModel.Load(file);
            var firstSourceSection = true;

            // Reuse the same mapping for importing to improve performance.
            var mapping = new ImportMapping(source, destination, false);

            foreach (var sourceSection in source.Sections)
            {
                // Import section from source document to destination document.
                var destinationSection = destination.Import(sourceSection, true, mapping);
                destination.Sections.Add(destinationSection);

                // Set the first section to start on the same page as the previous section.
                // In other words, the source content continues to flow with the current destination content.
                if (firstSourceSection)
                {
                    destinationSection.PageSetup.SectionStart = SectionStart.Continuous;
                    firstSourceSection = false;
                }
            }

            // Set the destination's default formatting to first source's default formatting.
            // Note, a single document can only have one default formatting.
            if (firstSourceDocument)
            {
                destination.DefaultCharacterFormat = source.DefaultCharacterFormat.Clone();
                destination.DefaultParagraphFormat = source.DefaultParagraphFormat.Clone();
                firstSourceDocument = false;
            }
        }

        // Save joined sections into one file.
        destination.Save("Merged Sections.%OutputFileType%");
    }
}
Imports GemBox.Document

Module Program

    Sub Main()

        ' If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        ' Word files that will be combined into one file.
        Dim files As String() =
        {
            "%#MergeFile01.docx%",
            "%#MergeFile02.docx%",
            "%#MergeFile03.docx%"
        }

        Dim destination As New DocumentModel()
        Dim firstSourceDocument = True

        For Each file In files

            Dim source = DocumentModel.Load(file)
            Dim firstSourceSection = True

            ' Reuse the same mapping for importing to improve performance.
            Dim mapping = New ImportMapping(source, destination, False)

            For Each sourceSection In source.Sections

                ' Import section from source document to destination document.
                Dim destinationSection = destination.Import(sourceSection, True, mapping)
                destination.Sections.Add(destinationSection)

                ' Set the first section to start on the same page as the previous section.
                ' In other words, the source content continues to flow with the current destination content.
                If firstSourceSection Then
                    destinationSection.PageSetup.SectionStart = SectionStart.Continuous
                    firstSourceSection = False
                End If
            Next

            ' Set the destination's default formatting to first source's default formatting.
            ' Note, a single document can only have one default formatting.
            If firstSourceDocument Then
                destination.DefaultCharacterFormat = source.DefaultCharacterFormat.Clone()
                destination.DefaultParagraphFormat = source.DefaultParagraphFormat.Clone()
                firstSourceDocument = False
            End If
        Next

        ' Save joined sections into one file.
        destination.Save("Merged Sections.%OutputFileType%")

    End Sub
End Module

See also


Next steps

GemBox.Document is a .NET component that enables you to read, write, edit, convert, and print document files from your .NET applications using one simple API. How about testing it today?

Download Buy