Cloning

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's duplicate into the same document, then you'll need to use the Element.Clone method to clone the element and insert its clone.

If you want to insert an element into another document, then you'll need to use one of the DocumentModel.Import methods as shown in the Importing example.

The following example shows how you can clone document elements (Section and Paragraph) together with their content (their descendant elements).

Word document with cloned or duplicated content
Screenshot of cloned document elements
Upload your file (Drag file here)
using System.Linq;
using GemBox.Document;

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

        var document = DocumentModel.Load("%InputFileName%");

        // Get first Section element.
        var section = document.Sections[0];

        // Get first Paragraph element.
        var paragraph = section.Blocks.OfType<Paragraph>().First();

        // Clone paragraph and add it to section.
        var cloneParagraph = paragraph.Clone(true);
        section.Blocks.Add(cloneParagraph);

        // Clone section and add it to document.
        var cloneSection = section.Clone(true);
        document.Sections.Add(cloneSection);

        document.Save("Cloning.%OutputFileType%");
    }
}
Imports System.Linq
Imports GemBox.Document

Module Program

    Sub Main()

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

        Dim document = DocumentModel.Load("%InputFileName%")

        ' Get first Section element.
        Dim section = document.Sections(0)

        ' Get first Paragraph element.
        Dim paragraph = section.Blocks.OfType(Of Paragraph).First()

        ' Clone paragraph and add it to section.
        Dim cloneParagraph = paragraph.Clone(True)
        section.Blocks.Add(cloneParagraph)

        ' Clone section and add it to document.
        Dim cloneSection = section.Clone(True)
        document.Sections.Add(cloneSection)

        document.Save("Cloning.%OutputFileType%")

    End Sub
End Module

When you want to generate multiple documents from the same document, you can use the DocumentModel.Clone method to perform a deep copy of the document.

By creating multiple document copies you're avoiding the need to load and parse the document from a file or a stream each time and thus improve the performance of document generation.

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Document yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.