Insert table in new document


#1

Hello,

I have a problem copying an existing table from my source document in to a newly created document, I get an exception "Cannot insert a node of this type at this location.". Could you please look at the code below and tell me what I'm doing wrong?

Document doc = new Document(@"C:\Quotation.doc");
DocumentBuilder builder = new DocumentBuilder(doc);

// Locate table in source document with a bookmark
if (builder.MoveToBookmark("MyBookMark"))
{
Node TableNode = builder.CurrentNode.GetAncestor(typeof(Aspose.Words.Table));
Aspose.Words.Table Table = ((Aspose.Words.Table)TableNode);

// Do something with Table here...

Document destDoc = new Document();
Node node = destDoc.ImportNode(TableNode, true, ImportFormatMode.KeepSourceFormatting);
destDoc.AppendChild(node); // Exception occurs here!

destDoc.Save("tmp.doc", SaveFormat.FormatDocument, Aspose.Words.SaveType.OpenInBrowser, Response);
}

Regards


#2

The problem occurs due to the fact that a table node cannot be a direct child of a document node. In your case table should be added to the Document \ Section \ Body children. For example:

destDoc.FirstSection.Body.AppendChild(node);

Please refer to programmer's guide help file to familiarize yourself with the document node hierarchy.

You can also use our DocumentExplorer source code demo to get a quick impression of how nodes are organized inside the document.

Best regards,


#3

Thank you very much for your quick reply! It solved all of my problems.