Hello, I have hundreds of existing template in my application and adding additional mergefield is a lot of work. What I need is to add additional merge field but dynamically. The template does not have bookmarks on the field that I need to insert the mergefield. Is there a way to add mergefield above the line as shown below. Thank you!
@JSAN28 You can use DocumentBuilder.InsertField method to insert merge fields into the document.
Unfortunately, it is difficult to provide you an exact code without sample document. If placeholders in your document are placed using whitespaces, I am afraid, it will be difficult or impossible to place merge fields automatically.
@alexey.noskov I use this code to insert merge field before the specified word.
DocumentBuilder builder = new DocumentBuilder(doc);
NodeCollection runs = doc.GetChildNodes(NodeType.Run, true);
foreach (Run run in runs)
{
if (run.Text.Contains("Street") && !run.Text.Contains("MERGEFIELD"))
{
builder.MoveTo(run);
builder.InsertField($"MERGEFIELD FieldStreet");
break;
}
}
Base on above code snippet it reproduce this output
I wonder if there’s a way to insert the merge field above the line.
@JSAN28 There is no concept of line
in MS Word document. Please see our documentation to learn more about document object model:
https://docs.aspose.com/words/net/aspose-words-document-object-model/