Unable to obtain FormFields in Word document

Hi, I’m having a bit of a problem with Aspose.Words for Java. Up until now, everything worked fine, but now I’m trying to obtain form fields from a Word document, and I’m coming up empty despite their presence in the Word document. I’m using the following code:

Document wordDocument = new Document(inputFilename);
FormFieldCollection fields = wordDocument.getRange().getFormFields();
log.debug(fields.getCount() + " form fields");

The log output from this code yields “0 form fields”. And I’m staring at the fields in the original document, which is quite short.

One possibility I’ve considered is that this is one of the limitations of the evaluation copy. However, I’ve been unable to find precise details on what the limitations are for unlicensed versions of the software, so I don’t know if this is an intentional limitation, a bug, or something that I’m doing wrong.

If it is an evaluation issue, I’d like to try the library with a temporary license, although I’m wondering if there will be any difficulty getting this to run in a Maven-based application.

Thanks in advance.

Ray Weidner

Hi Ray,

Thanks for your inquiry. Please note
that in evaluation mode there are some limitations applied. To avoid this you can request a free 30-day
trial license which removes these evaluation restrictions. You can
request this from here:
https://purchase.aspose.com/temporary-license

I would suggest you please use the latest version of Aspose.Words for Java 14.8.0. If you still face problem, please attach your input Word document here for testing. I will investigate the issue on my side and provide you more information.

rweidner:

If it is an evaluation issue, I’d like to try the library with a temporary license, although I’m wondering if there will be any difficulty getting this to run in a Maven-based application.

You can use Aspose.Words in Maven-based application. Please let us know if you face any issue.

Hi Tahir,

Thanks for the advice. Unfortunately, you didn’t really answer my question, which was whether the ability to obtain form fields is disabled in the evaluation version of the library. I haven’t seen anything to indicate that this would be the case, so I’m going to assume that the answer is “no.”

Anyway, I went ahead and grabbed the trial license and depended on the latest version, and tried it out again, and I’m still having the problem. I’ve added several form fields to a file, and the code above is not picking any of them up.

Any suggestions?

Ray Weidner

Hi Tahir,

Just noticed that. I’ll post a sample document shortly.

Ray Weidner

Hi Tahir,

I am attaching a Word document. When I run the code I listed in the original post, it reports finding zero fields. Please let me know if you get different results.

Ray Weidner

Hi Tahir,

Have you had any chance to try the sample document that I sent you?

Hi Ray,

Please accept my apologies for late response. I am working over your query and will update you shortly.

Hi Ray,

Thanks for your patience. Your document contains structured document tag (SDT or content control) not the FormField. You can interact with content controls by using StructuredDocumentTag class.
Structured document tags (SDTs) allow to embed customer-defined
semantics as well as its behavior and appearance into a document. You can get the collection of contents control by using Document.GetChildNodes method. See the following code snippet.

Document doc = new Document(MyDir + "fieldtest.docx");
foreach (StructuredDocumentTag std in doc.GetChildNodes(NodeType.StructuredDocumentTag, true))
{
    //Your code...
}

FormField
class represents a single form field. Microsoft Word provides the
following form fields: checkbox, text input and dropdown (combobox).

FormField
is an inline-node and can only be a child of Paragraph. FormField is
represented in a document by a special character and positioned as a
character within a line of text.

A complete form field in a Word
document is a complex structure represented by several nodes: field
start, field code such as FORMTEXT, form field data, field separator,
field result, field end and a bookmark. To programmatically create form
fields in a Word document use DocumentBuilder.InsertCheckBox,
DocumentBuilder.InsertTextInput and DocumentBuilder.InsertComboBox which
make sure all of the form field nodes are created in a correct order
and in a suitable state. Please
check the legacy form controls in attached image (LegacyForms.png).

From the Developer’s tab in MS Word, you can insert FormFields. Please read following web link ‘How to: Show the Developer Tab on the Ribbon’.
https://docs.microsoft.com/en-us/visualstudio/vsto/how-to-show-the-developer-tab-on-the-ribbon

Hope this answers your query. Please let us know if you have any more queries.

Hi Tahir,

Thank you very much for your response. It’s a bit embarrassing that I got the terminology wrong; I had thought I had created form fields as per online instructions from MS. But your solution makes it viable no matter what it’s called, so thanks for that. I’ll also take a look at your instructions on form fields, in case there’s some benefit to using them instead of SDTs.

Ray Weidner

Hi Ray,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.