Free Support Forum - aspose.com

Removing Word Form Field

Hi,

I am having problem with removing a form field in the Word document. The following is my code.

...

Dim field As FormFields = doc.Range.FormFields

'form field with bookmark "Name"

builder.MoveToBookmark("Name")

field.Item("Name").Remove()

builder.Write("John Doe")

...

When the document opens up, I see the value that was inserted, "John Doe", however the form field 'Name' is still in the document. Is there another way of removing the form field? Once the data is inserted, I don't want the users to see and edit the form field. Thanks.

Good spotting. FormField.Remove does not seem to delete the complete form field from the document.

A form field is represented in Aspose.Word.Document using several nodes. FormField.Remove is deleting only one node, not a complete form field. I left FormField.Remove without changes for now, will address it later, but I added FormFields.Remove and RemoveAt methods that do the job properly. It will be released in Aspose.Word 3.3.4 in the next 2-3 days.

This code will work:

[Test]

public void TestRemoveFormField()

{

Document doc = TestUtil.Open(@"Model\FormField\TestRemoveFormField.doc");

DocumentBuilder builder = new DocumentBuilder(doc);

doc.Range.FormFields.Remove("Text1");

builder.MoveToBookmark("Text1");

builder.Write("John Doe");

TestUtil.Save(doc, @"Model\FormField\TestRemoveFormField Out.doc");

Assert.AreEqual("John Doe\r\x000c", doc.GetText());

}