article

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

Email
Submitted on: 3/16/2016 6:58:10 AM
By: Sherazam  
Level: Intermediate
User Rating: Unrated
Compatibility: C#, VB.NET
Views: 2589
 
     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.

 
				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.
 


Other 44 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.