Cloning Elements in DOCX Files Using C# and VB.NET

If you need to clone elements in your Word files programmatically using C# or VB.NET, you can do that easily with the GemBox.Document library.

As shown on the content model diagram, there are many types of document elements and they all share the same. They all share the same Element base class.

Each element instance can be used only once in a document. For example, you cannot add the same run element to multiple paragraphs. To do that you'll need to clone the element, using the Element.Clone method, and then 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 Combining 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 the 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 the 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.

See also


Next steps

GemBox.Document is a .NET component that enables you to read, write, edit, convert, and print document files from your .NET applications using one simple API. How about testing it today?

Download Buy