Clone/copy table

Hi,
I’m trying to clone a table and insert several copies of the clone into multiple locations in the same document. For example:

Node SrcNode = Tables[5].Clone(true);
Node DestNode = Tables[0];
Table NewTable = (Table) DestNode.ParentNode.InsertAfter(SrcNode, DestNode);

This does work, but I have a couple of questions:

  1. I want to insert a line-break (basically an empty line) between the destination table and the inserted table (now, after inserting, the two tables have no space between them). How can I do that ? I’ve tried this:
Builder.MoveTo(DestNode.GetAncestor(NodeType.Body));
Builder.Writeln();

but I get an exception “The node must be a paragraph or a direct child of a paragraph”. If I use NodeType.Paragraph instead, the nodetype is null and I get a null exception.

  1. When trying to insert several copies of the same table, only the first one succeeds. For example, calling the InsertAfter method several times, only a single copy gets inserted. The Help file states that “If the newChild is already in the tree, it is first removed.” which might explain why. But then how can you accomplish this ? What constitutes a “new” child ?

Best regards and thanks,
Michael

Hi
Thanks for your inquiry.

  1. You can use InsertAfter method for inserting empty paragraph between tables. For example see the following code:
// Open document
Document doc = new Document(@"Test079\in.doc");
// Get tale form document
Table tab = doc.FirstSection.Body.Tables[0];
// Clone table
Node clone = tab.Clone(true);
// Create new Paragraph
Paragraph par = new Paragraph(doc);
// Insert this paragraph after original table
tab.ParentNode.InsertAfter(par, tab);
// Insert clone after paragraph
par.ParentNode.InsertAfter(clone, par);
// Save output document
doc.Save(@"Test079\out.doc");
  1. You should clone table each time you want to insert its copy into the document. For example see the following code:
// Insert 5 copies of table
for (int i = 0; i < 5; i++)
{
    // Clone table
    Node clone = tab.Clone(true);
    // Create new Paragraph
    Paragraph par = new Paragraph(doc);
    // Insert this paragraph after original table
    tab.ParentNode.InsertAfter(par, tab);
    // Insert clone after paragraph
    par.ParentNode.InsertAfter(clone, par);
}

Hope this helps.
Best regards.