3

I am trying to retrieve value from merge field using Docx4j in java. Currenty I'm retrieving all the contents of the word document using:

WordprocessingMLPackage newWordMLPackage = WordprocessingMLPackage
    .load(new java.io.File("C:/Users/admin/Desktop/test" + i + ".docx"));
MainDocumentPart documentPart = newWordMLPackage.getMainDocumentPart();                 
System.out.println(documentPart.getContent());

This returns a list of contents from the word document. What I'm currently getting is

MERGEFIELD lastName \* MERGEFORMAT himura

What I want is to get the value 'himura' from the merge-field 'lastName'. How can I achieve this?
Thanks

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Sujal
  • 671
  • 1
  • 16
  • 34

1 Answers1

0

You can do it using xpath - see method documentPart.getJAXBNodesViaXPath(xpath, false);

I had similar problem (wanted to replace MergeField with my own content). After long research, I wrote a method that can do it:

private void replaceTextWithElement(MainDocumentPart mainDocumentPart, String textToReplace, Collection<Object> newElements) throws JAXBException, Docx4JException {
        final String xpath = "//w:r[w:instrText[contains(text(),'MERGEFIELD') and contains(text(),'" + textToReplace + "')]]";
        final List<Object> foundNodes = mainDocumentPart.getJAXBNodesViaXPath(xpath, false);
        if (isEmpty(foundNodes)) {
            throw new RuntimeException("Cannot find textToReplace: \"" + textToReplace + "\" in document, skipping replacement.");
        }

        final R r = (R)foundNodes.get(0);
        final P parent = (P)r.getParent();
        final int index = mainDocumentPart.getContent().indexOf(parent);

        mainDocumentPart.getContent().remove(parent);
        if (newElements != null) {
            mainDocumentPart.getContent().addAll(index, newElements);
        }
    }
Greg Witczak
  • 1,634
  • 4
  • 27
  • 56