Hi
Thanks for your explanation. If I understood your requirements correctly you need move to TableStart mergefied to rename it. You can achieve this without DocumentBuilder. See the following code:
//Open document
Document doc = new Document(@"Test068\in.doc");
DocumentBuilder builder = new DocumentBuilder(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 thsi paragraph after original table
tab.ParentNode.InsertAfter(par, tab);
//Insert clone after paragraph
par.ParentNode.InsertAfter(clone, par);
//Now we whould like to rename TableStart and Table end mergefields
// Select all field start nodes so we can find the merge fields.
NodeCollection fieldStarts = (clone as CompositeNode).GetChildNodes(NodeType.FieldStart, true, false);
foreach (FieldStart fieldStart in fieldStarts)
{
if (fieldStart.FieldType.Equals(FieldType.FieldMergeField))
{
MergeField mergeField = new MergeField(fieldStart);
if(mergeField.Name == "TableStart:CustomerData")
mergeField.Name = "TableStart:CustomerDataRenamed";
if(mergeField.Name == "TableEnd:CustomerData")
mergeField.Name = "TableEnd:CustomerDataRenamed";
}
}
//Save output document
doc.Save(@"Test068\out.doc");
The implementation of MergeFieldClass you can find here:
http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/rename-merge-fields.html
of course you can move DocumentBuilder cursor to the merge field. But before moving you should insert cloned table into the document. In this case there will be few mergfield with same name and first DocumentBuilder cursor will be moved to the first occurrence.
You can use the following line of code to move to mergefield with name “TableStart:CustomerData”
builder.MoveToMergeField("CustomerData");
Best regards.
Alexey Noskov
Developer/Technical Support
Aspose Auckland Team