Importing

There are many types of document elements, as shown on the content model diagram. They all share the same Element base class and each element object can only exist in one place in a document.

If you want to insert an element into another document, then you'll need to first import it to the destination document and then insert the imported element.

If you want to insert an element's duplicate into the same document, then you'll need to use the Element.Clone method as shown in the Cloning example.

The following example demonstrates how you can import document elements into another document.

Screenshot of imported document elements
Word document with imported content from another document
Upload your file (Drag file here)
using GemBox.Document;

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

        var destinationDocument = DocumentModel.Load("%InputFileName%");
        var sourceDocument = DocumentModel.Load("%#Reading.docx%");

        // 1. The following is the easiest way how to import content from source to destination,
        // it will automatically imports the document's elements.
        //destinationDocument.Content.End.InsertRange(sourceDocument.Content);

        // 2. The following enables various customization.
        // For instance, we can change that the imported content doesn't start from new page
        // by setting "SectionStart.Continuous" on first destination section.

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

        // Import all sections from source document to destination document.
        foreach (var sourceSection in sourceDocument.Sections)
        {
            var destinationSection = destinationDocument.Import(sourceSection, true, mapping);
            destinationDocument.Sections.Add(destinationSection);
        }

        destinationDocument.Save("Importing.%OutputFileType%");
    }
}
Imports GemBox.Document

Module Program

    Sub Main()

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

        Dim destinationDocument = DocumentModel.Load("%InputFileName%")
        Dim sourceDocument = DocumentModel.Load("%#Reading.docx%")

        ' 1. The following is the easiest way how to import content from source to destination,
        ' it will automatically imports the document's elements.
        'destinationDocument.Content.End.InsertRange(sourceDocument.Content);

        ' 2. The following enables various customization.
        ' For instance, we can change that the imported content doesn't start from new page
        ' by setting "SectionStart.Continuous" on first destination section.

        ' Reuse the same mapping for importing to improve performance.
        Dim mapping As New ImportMapping(sourceDocument, destinationDocument, False)

        ' Import all sections from source document to destination document.
        For Each sourceSection In sourceDocument.Sections

            Dim destinationSection = destinationDocument.Import(sourceSection, True, mapping)
            destinationDocument.Sections.Add(destinationSection)

        Next

        destinationDocument.Save("Importing.%OutputFileType%")

    End Sub
End Module

Check next example or download examples from GitHub.