Deleting FormField

Hi,
I am using aspose word for java andcould not find a simple way to delete a formfield in document.
The approch I am using right now is to get bookmark around form field and them do bookmark.setText("") and then remove that bookmark. It works in most of the cases but if you don’t have bookmark around field it may fail.

Will it be possible for you to add remove() method in FormField class?

-Sunil

Somehow, FormField remove methods are not exposed in our public API. We will fix it in the next release which is due in about a week.



Sorry for inconvenience.

Hi
Is new version for Aspose Word for Java is out yet?
I was looking for delete/remove method for FormField.

Thanks
Sunil

The release is planned for the beginning of the next week.

Regards,

Aspose.Words for Java 1.0.3 is out. You can download, it provides the remove methods.

Thanks for the update.
I have downloaded 1.0.3 and it has remove method for form field.
I am trying to delete a formfield using new release as

Document doc = new Document("/data/Template-TEST.doc");
FormField field = doc.getRange().getFormFields().get(0);
field.remove();
doc.save(“C:/data/out.doc”);

I am not getting any compliation/ runtime errors using above code but also it is not deleteing form field.
I am attaching files used in sample code with it.

Do I need to do something else to remove this field?

Thanks
Sunil

Try

Document doc = new Document("/data/Template-TEST.doc");
FormField field = doc.getRange().getFormFields().get(0);
doc.getRange().getFormFields().remove(field);
doc.save("C:/data/out.doc");

The formField.remove() method does not work because FormField node is only part of the form field declaration. This declaration is basically a field and is comprised of FieldStart, FieldEnd nodes, FormField node proper and maybe also several SpecialChar and Run nodes.

To remove the entire form field declaration from the document you need to use doc.getRange().getFormFields().remove(...) or doc.getRange().getFormFields().removeAt(...) methods.

Best regards,

Hi Vladimir,

doc.getRange().getFormFields().remove(…) approch deletes all the part of form field except bookmark around it.
In most of cases MS Word inserts a bookmark around form field which stays there even after deleting form field using aspose. I am not sure if it is a bug or the product has been designed this way but if you can address this issue it will make development much easier and cleaner.

Thanks
-Sunil

Point taken. I have logged this to our defect base as issue #1078. We will try to add the enclosing bookmark removal to FormFields.Remove and RemoveAt functionality in the next version of Aspose.Words for Java.

Best regards,

Sorry, I decided no to change this behaviour now. Maybe someone wants the bookmark to stay when deleting the form field.

If you want to delete a bookmark after you delete a form field, just use doc.Range.Bookmarks.Remove(bookmarkName).