Home > Code > C# > How to Insert Clone of an Existing Table in a Word Document inside .NET Apps

How to Insert Clone of an Existing Table in a Word Document inside .NET Apps

by sher azam   on Mar 16, 2016   Category: C#   |  Views: 422    |  Points: 25   |  Gold 


This technical tip explains how .NET developers can insert a clone of an existing table in MS Word document inside .NET Applications. Often there are times when you have an existing table in a document and would like to add a copy of this table then apply some modifications. The easiest way to duplicate a table while retaining all formatting is to clone the table node using the Table.Clone method. If you are looking at creating tables in document which dynamically grow with each record from your data source, then the above method is not advised. Instead the desired output is achieved more easily by using Mail Merge with Regions. You can learn more about this technique under the topic Mail Merge with Regions. Below is the code sample shows how to insert a table using the constructors of nodes.

//Code samples Shows how to insert a table using the constructors of nodes

//[C# Code Sample]


// For complete examples and data files, please go to https://github.com/asposewords/Aspose_Words_NET

Document doc = new Document(dataDir + "Table.SimpleTable.doc");

// Retrieve the first table in the document.

Table table = (Table)doc.GetChild(NodeType.Table, 0, true);

// Create a clone of the table.

Table tableClone = (Table)table.Clone(true);

// Insert the cloned table into the document after the original

table.ParentNode.InsertAfter(tableClone, table);

// Insert an empty paragraph between the two tables or else they will be combined into one

// upon save. This has to do with document validation.

table.ParentNode.InsertAfter(new Paragraph(doc), table);

dataDir = dataDir + "Table.CloneTableAndInsert_out_.doc";

// Save the document to disk.

doc.Save(dataDir);


// [Visual Basic Code Sample]

' For complete examples and data files, please go to https://github.com/asposewords/Aspose_Words_NET


Dim doc As New Document(dataDir & Convert.ToString("Table.SimpleTable.doc"))


' Retrieve the first table in the document.


Dim table As Table = DirectCast(doc.GetChild(NodeType.Table, 0, True), Table)

' Create a clone of the table.


Dim tableClone As Table = DirectCast(table.Clone(True), Table)


' Insert the cloned table into the document after the original


table.ParentNode.InsertAfter(tableClone, table)


' Insert an empty paragraph between the two tables or else they will be combined into one


' upon save. This has to do with document validation.


table.ParentNode.InsertAfter(New Paragraph(doc), table)


dataDir = dataDir & Convert.ToString("Table.CloneTableAndInsert_out_.doc")

' Save the document to disk.

doc.Save(dataDir)

// Code sample Shows how to make a clone of the last row of a table and append it to the table.

//[C# Code Sample]

// For complete examples and data files, please go to https://github.com/asposewords/Aspose_Words_NET

Document doc = new Document(dataDir + "Table.SimpleTable.doc");

// Retrieve the first table in the document.

Table table = (Table)doc.GetChild(NodeType.Table, 0, true);

// Clone the last row in the table.

Row clonedRow = (Row)table.LastRow.Clone(true);

// Remove all content from the cloned row's cells. This makes the row ready for

// new content to be inserted into.

foreach (Cell cell in clonedRow.Cells)

cell.RemoveAllChildren();

// Add the row to the end of the table.

table.AppendChild(clonedRow);

dataDir = dataDir + "Table.AddCloneRowToTable_out_.doc";

// Save the document to disk.

doc.Save(dataDir);

// [Visual Basic Code Sample]

// [Visual Basic Code Sample]


' For complete examples and data files, please go to https://github.com/asposewords/Aspose_Words_NET

Dim doc As New Document(dataDir & Convert.ToString("Table.SimpleTable.doc"))

' Retrieve the first table in the document.

Dim table As Table = DirectCast(doc.GetChild(NodeType.Table, 0, True), Table)

' Clone the last row in the table.

Dim clonedRow As Row = DirectCast(table.LastRow.Clone(True), Row)

' Remove all content from the cloned row's cells. This makes the row ready for

' new content to be inserted into.

For Each cell As Cell In clonedRow.Cells

cell.RemoveAllChildren()

Next

' Add the row to the end of the table.


table.AppendChild(clonedRow)

dataDir = dataDir & Convert.ToString("Table.AddCloneRowToTable_out_.doc")


' Save the document to disk.

doc.Save(dataDir)



More about Aspose.Words for .NET

Aspose.Words is a word processing component that enables Java & .NET applications to read, write and modify Word documents without using Microsoft Word. Other useful features include document creation, content and formatting manipulation, mail merge abilities, reporting features, TOC updated/rebuilt, Embedded OOXML, Footnotes rendering and support of DOCX, DOC, WordprocessingML, HTML, XHTML, TXT and PDF formats (requires Aspose.Pdf). It supports both 32-bit and 64-bit operating systems. You can even use Aspose.Words to build applications with Mono.

- Homepage of Aspose.Words for .NET: http://www.aspose.com/.net/word-component.aspx

- Download Aspose.Words for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.words-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts