We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Can't delete form fields programmatically


Inspired by your example described here: https://docs.aspose.com/words/net/working-with-form-fields/#inserting-form-fields-in-microsoft-word

I’m trying to programmatically remove form fields, but I still see them in the saved result.

Please tell me what I’m doing wrong, also provide a working code example.

Here is a link to the file with the form fields:

My code:

var doc = new Document("Request for Modification-Fillable Form.docx");
var formFields = doc.Range.FormFields;
foreach (var field in formFields.ToList())
    // or 
    // field.RemoveField();
// or 
// formFields.Clear();
doc.Save("result.docx", SaveFormat.Docx);

@ZakharS Range.FormFields returns only legacy form fields. Your document contains both legacy form fields and content controls (structured document tags). To remove them both your should use code like this:

Document doc = new Document(@"C:\Temp\in.docx");

// Remove legacy form fields.
// Remove SDTs. Use RemoveSelfOnly method to leave inner content is the SDTs in the document.
doc.GetChildNodes(NodeType.StructuredDocumentTag, true).Cast<StructuredDocumentTag>().ToList()
    .ForEach(sdt => sdt.RemoveSelfOnly());