Page 1 of 1

Replacing text inside text boxes

PostPosted: Thu Apr 28, 2016 10:42 pm
by borja
Hi everyone,

I am a novice in docx4j. I'm trying to replace text inside text boxes.

I already saw this topic docx-java-f6/changing-text-in-a-textbox-t1523.html

But when i tried, not only the strings were not replaced, the text boxes were deleted in the output file. The same occurs with other shapes like some horizontal lines. My code is very simple.

Code: Select all
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File("template.docx"));
            MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("RAZON", RAZON);
            map.put("DIR", DIR);

            documentPart.variableReplace(map);
            File f = new File("output2.docx");
            wordMLPackage.save(f);


In template.docx have 4 text boxes. two of them contains ${RAZON} and {DIR} texts. But the 4 are deleted during processing.

Im using 3.2.2 version.

Any help i really appreciate. Thanks.

Re: Replacing text inside text boxes

PostPosted: Fri Apr 29, 2016 4:08 am
by borja
I tried to edit manually the xml inside docx zip to group tags but, again, all text boxes and shapes are destroyed.

I finally resolved this using merge mail fields. Very easy to implement and i think, more robust.

Thanks for this great library.