Support Center

Join / Combine multiple documents into a single file

In order to join multiple documents together to create a single document, we need to add all the content from these source documents into a destination document.
It is not allowed to directly insert an element instance from one document to another; we need to first import these elements to another document and then insert them into a specified place.

So to achieve a documents joining, we can loop through source documents sections, import them to a destination document, and insert them at the end.

Here is an extension method which will achieve this task:

C# code

public static class GemBoxDocumentHelper
    public static DocumentModel JoinWith(this DocumentModel destinationDocument, string filePath)
        DocumentModel sourceDocument = DocumentModel.Load(filePath);
        foreach (Section sourceSection in sourceDocument.Sections)
            Section destinationSection = destinationDocument.Import(sourceSection, truefalse);
        return destinationDocument;

VB.NET code

Module GemBoxDocumentHelper
    Public Function JoinWith(destinationDocument As DocumentModel, filePath As StringAs DocumentModel
        Dim sourceDocument As DocumentModel = DocumentModel.Load(filePath)
        For Each sourceSection As Section In sourceDocument.Sections
            Dim destinationSection As Section = destinationDocument.Import(sourceSection, TrueFalse)
        Return destinationDocument
    End Function
End Module

And here is how we can use it:

C# code

string filePath1 = "In1.docx";
string filePath2 = "In2.docx";
string filePath3 = "In3.docx";
string filePath4 = "Out.docx";


VB.NET code

Dim filePath1 As String = "In1.docx"
Dim filePath2 As String = "In2.docx"
Dim filePath3 As String = "In3.docx"
Dim filePath4 As String = "Out.docx"

DocumentModel.Load(filePath1) _
             .JoinWith(filePath2) _
             .JoinWith(filePath3) _

4 of 5 people found this page helpful
Subscribe to this article to get an email notification when it is updated.


  • There are no comments.