问题
I am using docx4j 2.8.1 to replace texts and a logo within the word files. Now I want to improve performance since the processing of about 50 documents is quite slow. What options are recommended to increase performance of docx4j?
The code to replace the strings (not the logo) looks like:
String xml = XmlUtils.marshaltoString(documentPart.getJaxbElement(), true);
HashMap<String, String> mappings = buildReplaceMap(userData);
Object obj = XmlUtils.unmarshallFromTemplate(xml, mappings);
documentPart.setJaxbElement(obj);
回答1:
Please try http://www.docx4java.org/docx4j/docx4j-nightly-20130328.jar
It contains an experimental variableReplace method. Assuming documentPart and mappings as per your question, you can call:
documentPart.variableReplace(mappings);
You don't need documentPart.setJaxbElement.
In my testing with a 175 page document, processing took 1/3 sec (roughly 3 times faster).
Any further performance improvements will come most easily from providing more resources (more cores, if you want to do it concurrently), and more RAM.
Let me know what you think.
来源:https://stackoverflow.com/questions/15664726/optimize-docx4j-performance