I have a document that I want to iterate through and replace each form field with static text. Like FORM FIELD REMOVED. I see that I can use a DocumentBuilderv object and use builder.MoveToBookmarkbuilder.MoveToBookmark then builder.Writebuilder.Write … BUT … not all form fields have names. Some are actually empty. How can insert my text (before or after) every single form field in the document.
Here is what “amlmost” works (it does not work if the form field name is empty):
DocumentBuilder builder = new DocumentBuilder(dest);
foreach(Aspose.Words.Fields.FormField ff in dest.Range.FormFields)
{
builder.MoveToBookmark(ff.Name);
builder.Write("FORM FIELD REMOVED");
}
// ... followed by some code that just calls FormFields.RemoveAt on every form field.
Hi
Thanks for your request. I think, you can try using code like the following:
Document doc = new Document(@"Test001\in.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert static text at place of formfields.
foreach(FormField ff in doc.Range.FormFields)
{
if (string.IsNullOrEmpty(ff.Name))
{
// If there is no bookmark asociated with the formfield,
// We should move to the corresponding field start.
Node currentNode = ff;
while (currentNode != null && currentNode.NodeType != NodeType.FieldStart)
{
currentNode = currentNode.PreviousSibling;
}
// Move to the current node if it is not null otherwise move to ff.
builder.MoveTo(currentNode != null ? currentNode : ff);
}
else
{
builder.MoveToBookmark(ff.Name, false, true);
}
builder.Write("FORM FIELD REMOVED");
}
// Remove all formfields.
doc.Range.FormFields.Clear();
doc.Save(@"Test001\out.doc");
Hi
Thanks for your request. Yes, this should work. But I suppose it would be better to leave code as is. If a form field has a bookmark associated with it; it will be faster to move to bookmark directly than find the FieldStart node.
Best regards,