Tuesday, 15 March 2011

java - VariableReplace - No Errors with No Result -



java - VariableReplace - No Errors with No Result -

i'm using docx4java 3.1.0. have dependencies included in netbeans project.

when run piece of code:

bundle pse; /** * * @author hrach */ import java.util.hashmap; import org.docx4j.xmlutils; import org.docx4j.jaxb.context; import org.docx4j.openpackaging.io.savetozipfile; import org.docx4j.openpackaging.packages.wordprocessingmlpackage; import org.docx4j.openpackaging.parts.wordprocessingml.maindocumentpart; import org.docx4j.wml.document; public class variablereplace { public static void main(string[] args) throws exception { // exclude context init timing org.docx4j.wml.objectfactory foo = context.getwmlobjectfactory(); // input docx has variables in it: ${colour}, ${icecream} string inputfilepath = system.getproperty("user.dir") + "/documents/in.docx"; boolean save = false; string outputfilepath = system.getproperty("user.dir") + "/rd/out_variablereplace.docx"; wordprocessingmlpackage wordmlpackage = wordprocessingmlpackage .load(new java.io.file(inputfilepath)); maindocumentpart documentpart = wordmlpackage.getmaindocumentpart(); hashmap<string, string> mappings = new hashmap<string, string>(); mappings.put("colour", "green"); mappings.put("icecream", "chocolate"); long start = system.currenttimemillis(); // approach 1 (from 3.0.0; faster if haven't yet caused unmarshalling occur): documentpart.variablereplace(mappings); long end = system.currenttimemillis(); long total = end - start; system.out.println("time: " + total); // save if (save) { savetozipfile saver = new savetozipfile(wordmlpackage); saver.save(outputfilepath); } else { system.out.println(xmlutils.marshaltostring(documentpart.getjaxbelement(), true, true)); } } }

i output below:

run: slf4j: failed load class "org.slf4j.impl.staticloggerbinder". slf4j: defaulting no-operation (nop) logger implementation slf4j: see http://www.slf4j.org/codes.html#staticloggerbinder farther details. time: 9 <w:document mc:ignorable="w14 w15 wp14" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:ns21="urn:schemas-microsoft-com:office:powerpoint" xmlns:ns23="http://schemas.microsoft.com/office/2006/coverpageprops" xmlns:dsp="http://schemas.microsoft.com/office/drawing/2008/diagram" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:odx="http://opendope.org/xpaths" xmlns:odgm="http://opendope.org/smartart/datahierarchy" xmlns:dgm="http://schemas.openxmlformats.org/drawingml/2006/diagram" xmlns:ns17="urn:schemas-microsoft-com:office:excel" xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:odi="http://opendope.org/components" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:ns9="http://schemas.openxmlformats.org/schemalibrary/2006/main" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingdrawing" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:ns32="http://schemas.openxmlformats.org/drawingml/2006/lockedcanvas" xmlns:m="http://schemas.openxmlformats.org/officedocument/2006/math" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:ns30="http://schemas.openxmlformats.org/officedocument/2006/bibliography" xmlns:ns12="http://schemas.openxmlformats.org/drawingml/2006/chartdrawing" xmlns:ns31="http://schemas.openxmlformats.org/drawingml/2006/compatibility" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:odq="http://opendope.org/questions" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:r="http://schemas.openxmlformats.org/officedocument/2006/relationships" xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetdrawing" xmlns:odc="http://opendope.org/conditions" xmlns:oda="http://opendope.org/answers"> <w:body> <w:p w:rsidrpr="00b959f9" w:rsidr="00d133f0" w:rsidp="00b959f9" w:rsidrdefault="00b959f9"> <w:r w:rsidrpr="00b959f9"> <w:t></w:t> </w:r> <w:r> <w:t>colour</w:t> </w:r> <w:r w:rsidrpr="00b959f9"> <w:t></w:t> </w:r> <w:r> <w:t xml:space="preserve">, </w:t> </w:r> <w:r w:rsidrpr="00b959f9"> <w:t></w:t> </w:r> <w:r> <w:t>icecream</w:t> </w:r> <w:bookmarkstart w:name="_goback" w:id="0"/> <w:bookmarkend w:id="0"/> <w:r w:rsidrpr="00b959f9"> <w:t></w:t> </w:r> </w:p> <w:sectpr w:rsidrpr="00b959f9" w:rsidr="00d133f0"> <w:pgsz w:w="11906" w:h="16838"/> <w:pgmar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/> <w:cols w:space="708"/> <w:docgrid w:linepitch="360"/> </w:sectpr> </w:body> </w:document> build successful (total time: 10 seconds)

the user directory is: c:\users\hrach\desktop\programming\java projects\pse documents folder is: c:\users\hrach\desktop\programming\java projects\pse\documents rd folder is: c:\users\hrach\desktop\programming\java projects\pse\rd

i have made simple docx file , included these variables in it: ${colour}, ${icecream}

when run file, expecting output file made in directory rd variables replaced accordingly. reason, unable work, because microsoft word splitting words?

please help , in advance, hrach.

just set 'save'flag true. default illustration contains false.

boolean save = false;

change to:

boolean save = true;

java docx4j

No comments:

Post a Comment