Cloning

Document element instances in GemBox.Document can be only at one single location in a document at a time. To insert a document element into some other part of the same document, it must be cloned and the clone should be inserted.

Following sample demonstrates how to clone document elements, first by cloning a section and its content, and then by individually cloning section content elements – blocks.

Screenshot

Cloning 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("Reading.docx");
16
17        // Clone section
18        document.Sections.Add(document.Sections[0].Clone(true));
19        
20        // Clone paragraphs
21        foreach (Block item in document.Sections[0].Blocks)
22            document.Sections.Last().Blocks.Add(item.Clone(true));
23
24        document.Save("Cloning.docx");
25    }
26}
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("Reading.docx")
15
16        ' Clone section
17        document.Sections.Add(document.Sections(0).Clone(True))
18
19        ' Clone paragraphs
20        For Each item As Block In document.Sections(0).Blocks
21            document.Sections.Last().Blocks.Add(item.Clone(True))
22        Next
23
24        document.Save("Cloning.docx")
25
26    End Sub
27
28End Module

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