Importing

Document element instances from one document instance cannot be inserted to another document instance. They must first be imported to another document, and then the imported elements can be inserted.

Following sample shows how to import a document element into another document.

Screenshot

Importing Screenshot

See the full code below, use RUN EXAMPLE to execute.

Upload your file (Drag files here)

1using System;
2using System.IO;
3using System.Linq;
4using System.Text;
5using GemBox.Document;
6
7class Sample
8{
9    [STAThread]
10    static void Main(string[] args)
11    {
12        // If using Professional version, put your serial key below.
13        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
14
15        DocumentModel document = DocumentModel.Load("Invoice.docx");
16
17        string pathToFileDirectory = "Resources";
18
19        DocumentModel sourceDocument = DocumentModel.Load(Path.Combine(pathToFileDirectory, "Reading.docx"), LoadOptions.DocxDefault);
20
21        // Reuse same mapping for importing all sections to improve performance.
22        var mapping = new ImportMapping(sourceDocument, document, false);
23
24        // Import all sections from source document.
25        foreach (Section sourceSection in sourceDocument.Sections)
26        {
27            Section destinationSection = document.Import(sourceSection, true, mapping);
28            document.Sections.Add(destinationSection);
29        }
30
31        document.Save("Importing.docx");
32    }
33}
1Imports System
2Imports System.IO
3Imports System.Linq
4Imports System.Text
5Imports GemBox.Document
6
7Module Samples
8
9    Sub Main()
10
11        ' If using Professional version, put your serial key below.
12        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
13
14        Dim document As DocumentModel = DocumentModel.Load("Invoice.docx")
15
16        Dim pathToFileDirectory As String = "Resources"
17
18        Dim sourceDocument As DocumentModel = DocumentModel.Load(Path.Combine(pathToFileDirectory, "Reading.docx"), LoadOptions.DocxDefault)
19
20        ' Reuse same mapping for importing all sections to improve performance.
21        Dim mapping = New ImportMapping(sourceDocument, document, False)
22
23        ' Import all sections from source document.
24        For Each sourceSection As Section In sourceDocument.Sections
25            Dim destinationSection As Section = document.Import(sourceSection, True, mapping)
26            document.Sections.Add(destinationSection)
27        Next
28
29        document.Save("Importing.docx")
30
31    End Sub
32
33End Module

Check next sample or find out more about GemBox.Document and GemBox Software.