Bublik
Bublik

Reputation: 922

Get all mailMerge fields from docx using docx4J

I have .docx file, containing mailMerge fields. I want to extract all field names to List. I saw some examples of dox4J, how to replace these fields with mapped value, but I DO NOT want to replace them, I need to read them only.

Is there a semy easy way to do that using docx4J?

Upvotes: 2

Views: 1482

Answers (1)

JasonPlutext
JasonPlutext

Reputation: 15878

Have a look at https://github.com/plutext/docx4j/blob/master/src/samples/docx4j/org/docx4j/samples/FieldsDiagnostics.java

You can also look at the code in MailMerger:

    // find fields
    ComplexFieldLocator fl = new ComplexFieldLocator();
    new TraversalUtil(shellClone, fl);      

    // canonicalise and setup fieldRefs 
    List<FieldRef> fieldRefs = new ArrayList<FieldRef>();
    canonicaliseStarts(fl, fieldRefs);

    // Populate
    for (FieldRef fr : fieldRefs) {

        if ( fr.getFldName().equals("MERGEFIELD") ) {

            String instr = extractInstr(fr.getInstructions() );
            String datafieldName = getDatafieldNameFromInstr(instr);

Upvotes: 0

Related Questions