Find Field Properties

Contents
[ ]

A field which is inserted using DocumentBuilder.InsertField returns a Field object. This is a facade class which provides useful methods to quickly find such properties of a field.

The following code example shows how to find the field code and field result:

Note if you are only looking for the names of merge fields in the document then you can instead use the built-in method GetFieldNames.

The following code example shows how to get names of all merge fields in a document.


FAQ

  1. Q: How can I retrieve the field code of a field in Aspose.Words for Java?
    A: After obtaining a Field object (e.g., via DocumentBuilder.insertField or by iterating through the document), call field.getFieldCode(). This method returns the complete field code string, including the field type and its arguments.

  2. Q: How do I get the evaluated result (display text) of a field?
    A: Use the field.getResult() method on the Field instance. It returns the current result of the field after evaluation, which is the text that would appear in the document.

  3. Q: How can I list all merge field names in a document?
    A: Call document.getMailMerge().getFieldNames(). This method returns a String[] containing the names of every MERGEFIELD present in the document, making it easy to enumerate them.

  4. Q: How can I determine the type of a field (e.g., MERGEFIELD, REF) while iterating?
    A: Inspect the field code returned by field.getFieldCode(); the first word indicates the field type. Alternatively, you can use field.getType() which returns a FieldType enum value representing the specific field type.

  5. Q: Can I retrieve field properties from fields located in headers or footers?
    A: Yes. Access the header/footer nodes via section.getHeadersFooters(), then iterate their child nodes to locate Field objects. Once you have the Field, getFieldCode() and getResult() work the same way as for body fields.