Would you be so kind as to give me a little code snippet on how I could loop through a Word document and remove all of the “ASK” fields? I’m sure that would help with “FILLIN” fields too, right?
I’m trying to merge to a set of templates that may have these and I need to get rid of them!
Hello Mike!
Thank you for your inquiry.
Fields in Aspose.Words document model consist of several sequential nodes from FieldStart to FieldEnd including them. There is no one-call API to remove field of any kind. If you are sure that the fields subjected for removal fit in one paragraph each and don’t contain nested fields then you can try this simple function:
private static void RemoveSomeFields()
{
Document doc = new Document("source.doc");
// Collect all FieldStart nodes. We'll find and remove some nodes at these points.
NodeCollection startNodes = doc.GetChildNodes(NodeType.FieldStart, true);
foreach(FieldStart start in startNodes)
{
if ((start.FieldType == FieldType.FieldAsk) || (start.FieldType == FieldType.FieldFillIn))
{
Node curNode = start;
while (curNode != null)
{
// We should first get the next node then remove this one.
// This is because Remove() breaks the chain.
Node nextNode = curNode.NextSibling;
curNode.Remove();
if (curNode.NodeType == NodeType.FieldEnd)
break;
curNode = nextNode;
}
}
}
doc.Save("result.doc");
}
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.