0
votes

Im new to Docx4j and my task is to replace some Text of a docx Template. I read the getting Started Guide of docx4j but I don't think I fully understood the whole concept. Well Anyway... I already tried [the unmashalling Template of Docx4j][1], which worked fine with the given docx, but then I got the same Problem when I tried it on my own template The Exceptions say, that the HashMap doesnt contain valid keys or values, and therefore it doesnt replace the placeholders.

I replaced the

<w:proofErr w:type="spellEnd"/>

by disabling the spellchecking, but it still didn't work... And it also takes quite some time to run the app.

In didn't understand the databound example in the Getting_Started.pdf, so I'm running out of options...

How can I simply replace some String-Texts from a docx?

EDIT: I found out that if I add some Text to the unmarshallFromTemplate.docx and save it, that it wont replace the new lines of text. the - Tags are somehow splitted into multiple Tags:

<w:p w:rsidR="002512F8" w:rsidRDefault="002512F8" w:rsidP="002512F8"><w:r><w:t>My</w:t></w:r><w:r w:rsidR="001A5174"><w:t xml:space="preserve"> favourite ice cream is ${DEGREE</w:t></w:r><w:r><w:t>}.</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p>

editing the Text in the document.xml, and adding the missing Information didnt help much.

well anyway here is the document.xml of the Template.docx that im using: http://uploaded.net/file/vz4qr23o

EDIT 2:

Well guys. I found a quite suitable workaround for myself and dont know why it took so long to figure it out. As I was saying: The runs where splited up, and the reason for this was the ${} in my opinion. Therefore I simply used a # before my Placeholders and rewrote every placeholder, so that it would all be in one run.

Had to switch couple of times to the document.xml and rewrite the passages but then it worked. Then I simply used a replace(placeholder, xml) and replaced the text of the marshalled document.xml, then I unmarshalled it again. Worked. End of Story, fuck the nightly build or the mappings. THX

2
Please fill this question with all required data; posting a link to other forums on which you asked it is not a good practice.Dariusz

2 Answers

2
votes

docx4j source code has been on GitHub for a while now; that svn repository is obsolete.

The equivalent sample is now called VariableReplace. That code is a bit more efficient, but you need to build it yourself, or use a current nightly build.

You'll probably find running VariablePrepare addresses your issue.

1
votes

The placeholder search and replace code built in to docx4j works just fine, but if you're having issues with placeholders getting broken up by rsid entities, you need to ensure that you have grammar and spell-checking disabled when saving your "template" (i.e. source) document. This will help prevent your text runs becoming fragmented (note that you might want to disable proof-reading too, as that inserts bookmark tags here there and everywhere).

Once you've done the search and replace and have a new / updated document, you can re-enable spell-checking easily enough. This thread has more on RSIDs: turnoff rsid's spell check & grammar check in generated xml