Iterate merge field names in docx excluding those that are part of a region

Hi,

Using Java.
Say I have a document like so.

{{ Image:CompanyLogo }}
{{ CompanyName }}

{{ CompanyAddress }}

{{ #foreach Order }}{{ Date }} - {{ Invoice ID }}{{ /foreach Order }}

How would I get all field names that are not part of a foreach?

doc.getMailMerge().getFieldNames();

Gets me the ones in the loop as well.

These don’t work.

doc.getMailMerge().getFieldNamesForRegion(null);
doc.getMailMerge().getFieldNamesForRegion("");

Is it possible to do this with existing library functions?

@LiamMitchell

Thanks for your inquiry. We will appreciate it if you please share your sample document here as ZIP file. We will look into it and will guide you exactly.

Sample.zip (3.9 KB)

I would like to be able to use code to get each merge field that is not part of the nested foreach.

Example:

[ 'Image:CompanyLogo', 'CompanyName', 'CompanyAddress' ]

It’s okay if they are not returned as a String, so long as there is a collection I can iterate and get the textual content of each field.

Thanks

@LiamMitchell

Thanks for sharing the sample document. You may try this code and customize it as per your requirements. Hopefully it will help you to accomplish the task.

com.aspose.words.Document doc = new com.aspose.words.Document("Sample.docx");
DocumentBuilder builder = new DocumentBuilder();
doc.getMailMerge().setUseNonMergeFields(true);
List<String> list = new ArrayList<String>();

for (String item : doc.getMailMerge().getFieldNames())
{
    if (item.contains("TableStart"))
        break;
    else
        list.add(item);
 }
for (String field : list)
{
    System.out.println(field);
}