How to distinguish two form field element (checkbox or input) with the same form name in word

Our word documents has many form elements like inputs and checkboxes, unfortunately, some of them have the same field name, we need to distinguish them using aspose.words API.

I want to arrange them in different arrays, and then with each array, read/write each element in the order it occurs in the word document.

Is there anyone telling me how to do it with aspose.words API? thanks very much!

Hi Liu,

Thanks for your inquiry. You can use FormField.Type property for this purpose. The following code shows how to work with form field name, type, and result:

Document doc = new Document(MyDir + "FormFields.doc");
FormField formField = doc.Range.FormFields[3];
if (formField.Type.Equals(FieldType.FieldFormTextInput))
    formField.Result = "My name is " + formField.Name;

I hope, this helps.

Best regards,

Hi Awais Hafeez,

Thanks for your reply. I know how to use the basic API with form field name, type, and result. What I don’t know is, how to recognize each element that has the same form field name.

For example, there are thirty form field elements in “FormFields.doc” in total, eight of which share the same form field name ‘form1’, I want to extract the eight form fields in the order they occurred in the document to an array list, and then read/write them one by one. I know the basic model for Aspose.Words is DOM, is there any searching API that can search elements by field name and return the result to a list? or how can do it with basic API? thanks a lot.

By the way, I’m using Java edition of Aspose.Words.

Hi Liu,

Thanks for your inquiry and sorry for the delayed response. Please attach your input Word document here for testing. I will investigate the structure of this document and provide you more information.

Best regards,

Hi Awais Hafeez,

Please see attached word. In this word, there are three checboxes with the duplicate name ‘Check1’ and 12 inputs with the duplicate name ‘Text5’.

In order to distinguish each other with the same name, I intend to arrange them to two array: one is an checkbox array that contains three elements with the same name ‘Check1’, another is an input array that contains three elements with the same name ‘Text5’, the order of the elements in this array is consistent with the order they occured in the word.
.But I don’t know how to do it. I see the Document.selectNodes method that can search elemntes with xPath which return NodeList and tried following code:

Document doc = new Document("e:/test/same2.doc");
NodeList lst=doc.selectNodes("//FormField[@name='Check1']");

but it reported errors:

Exception in thread "main" java.lang.IllegalStateException: asposewobfuscated.ëZVY: attribute
at com.aspose.words.ëVA.ëW(Unknown Source)
at com.aspose.words.CompositeNode.selectNodes(Unknown Source)
at cn.gwssi.idep.opd.WordParser.main(WordParser.java:950)
Caused by: asposewobfuscated.ëZVY: attribute
at asposewobfuscated.ëZWE.ëZ6(Unknown Source)
at asposewobfuscated.ëZTJ.ëX(Unknown Source)
at asposewobfuscated.ëZUS.ëW(Unknown Source)
at asposewobfuscated.ëZUY.ëY(Unknown Source)
at asposewobfuscated.ëZV8.ëY(Unknown Source)
at asposewobfuscated.ëZUM.ëY(Unknown Source)
at asposewobfuscated.ëZTW.ëZ(Unknown Source)
at asposewobfuscated.ëZTW.ëZ(Unknown Source)
at asposewobfuscated.ëZUS.ëW(Unknown Source)
at asposewobfuscated.ëZUY.ëY(Unknown Source)
at asposewobfuscated.ëZVG.ëY(Unknown Source)
at asposewobfuscated.ëZUC.ëV(Unknown Source)
at asposewobfuscated.ëZWH.ëZ(Unknown Source)
at asposewobfuscated.ëZWH.ëZf(Unknown Source)
at com.aspose.words.ëVA.ëW(Unknown Source)
... 2 more

So how to write the Xpath expression?

Or Is there any other method to handle the duplicated elements on by one in order? Thanks.

Hi Liu,

Thanks for your inquiry. Please try running the following code to achieve what you’re looking for:

// Load Word document into DOM
Document doc = new Document("C:\Temp\inputs%26checkboxes+with+the+same+name.doc");
// Get a collection of all FormField objects in document
NodeCollection fields = doc.getChildNodes(NodeType.FORM_FIELD, true);
// repeatingFFs will hold FormFields with same name in the order they occur in document.
ArrayList repeatingFFs = new ArrayList();
// Gather info as to how many times a field with a particular name is repeated
Map<String, Integer> occurrences = new HashMap<String, Integer>();
for (FormField ff : (Iterable<FormField>)fields)
    occurrences.put(ff.getName(), occurrences.containsKey(ff.getName()) ? occurrences.get(ff.getName()) + 1 : 1);
// If a field name occurs more than once, locate such FormFields in document and add to the ArrayList
for (Map.Entry<String, Integer> entry : occurrences.entrySet())
    if (entry.getValue() > 1)
        for (FormField ff : (Iterable<FormField>)fields)
            if (ff.getName().equals(entry.getKey()))
                repeatingFFs.add(ff);

I hope, this helps.

Best regards,