I have text fields, and I also have to populate a checkbox.
To do the text fields, I use this method:
- Code: Select all
HashMap<String, String> tokens = new HashMap<String, String>();
tokens.put("TOKEN_HOLDER", MiscSiteConfig.getCurrentVAPrivacyOfficer().getName().toString());
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(checkList));
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
org.docx4j.wml.Document wmlDocumentEl = mainDocumentPart.getJaxbElement();
String xml = XmlUtils.marshaltoString(wmlDocumentEl, true);
Document doc = (Document) XmlUtils.unmarshallFromTemplate(xml, tokens);
mainDocumentPart.setJaxbElement(doc);
This seems to work fine by itself
I also have code to populate the checkbox. This code also works by itself.
- Code: Select all
String xpath = "//w:r[w:t[contains(text(),'${TOKEN_HOLDER_CHECKBOX}')]]";
List<Object> list = mainDocumentPart.getJAXBNodesViaXPath(xpath, false);
R r = (R) list.get(0);
addUncheck(r, "Check1");
And here is the addUncheck method. I know this isn't the best way but it was the easiest to learn since I could just drop in the XML.
- Code: Select all
public static void addUncheck(R r, String checkboxName) throws JAXBException {
R uncheck1 = (R) XmlUtils.unmarshalString(
"<w:r " +
"xmlns:ns25=\"http://schemas.openxmlformats.org/drawingml/2006/compatibility\" " +
"xmlns:ns26=\"http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas\" " +
"xmlns:ns24=\"http://schemas.openxmlformats.org/officeDocument/2006/bibliography\" " +
"xmlns:dsp=\"http://schemas.microsoft.com/office/drawing/2008/diagram\" " +
"xmlns:w10=\"urn:schemas-microsoft-com:office:word\" " +
"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:powerpoint\" " +
"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:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" " +
"xmlns:o=\"urn:schemas-microsoft-com:office:office\" " +
"xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\" " +
"xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" " +
"xmlns:ns13=\"urn:schemas-microsoft-com:office:excel\" " +
"xmlns:ns6=\"http://schemas.openxmlformats.org/schemaLibrary/2006/main\" " +
"xmlns:odq=\"http://opendope.org/questions\" " +
"xmlns:ns11=\"http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing\" " +
"xmlns:ns8=\"http://schemas.openxmlformats.org/drawingml/2006/chartDrawing\" " +
"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:odc=\"http://opendope.org/conditions\" " +
"w:rsidRPr=\"002655B2\">" +
"<w:rPr>" +
"<w:sz w:val=\"20\"/>" +
"<w:szCs w:val=\"20\"/>" +
"</w:rPr>" +
"<w:fldChar w:fldCharType=\"begin\">" +
"<w:ffData>" +
"<w:name w:val=\"" + checkboxName + "\"/>" +
"<w:enabled/>" +
"<w:calcOnExit w:val=\"false\"/>" +
"<w:checkBox>" +
"<w:sizeAuto/>" +
"<w:default w:val=\"false\"/>" +
"</w:checkBox>" +
"</w:ffData>" +
"</w:fldChar>" +
"</w:r>"
);
R uncheck2 = (R) XmlUtils.unmarshalString(
"<w:r " +
"xmlns:ns25=\"http://schemas.openxmlformats.org/drawingml/2006/compatibility\" " +
"xmlns:ns26=\"http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas\" " +
"xmlns:ns24=\"http://schemas.openxmlformats.org/officeDocument/2006/bibliography\" " +
"xmlns:dsp=\"http://schemas.microsoft.com/office/drawing/2008/diagram\" " +
"xmlns:w10=\"urn:schemas-microsoft-com:office:word\" " +
"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:powerpoint\" " +
"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:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" " +
"xmlns:o=\"urn:schemas-microsoft-com:office:office\" " +
"xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\" " +
"xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" " +
"xmlns:ns13=\"urn:schemas-microsoft-com:office:excel\" " +
"xmlns:ns6=\"http://schemas.openxmlformats.org/schemaLibrary/2006/main\" " +
"xmlns:odq=\"http://opendope.org/questions\" " +
"xmlns:ns11=\"http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing\" " +
"xmlns:ns8=\"http://schemas.openxmlformats.org/drawingml/2006/chartDrawing\" " +
"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:odc=\"http://opendope.org/conditions\" " +
"w:rsidR=\"004E5A4D\" w:rsidRPr=\"002655B2\">" +
"<w:rPr>" +
"<w:sz w:val=\"20\"/>" +
"<w:szCs w:val=\"20\"/>" +
"</w:rPr>" +
"<w:instrText xml:space=\"preserve\"> FORMCHECKBOX </w:instrText>" +
"</w:r>"
);
R uncheck3 = (R) XmlUtils.unmarshalString(
"<w:r " +
"xmlns:ns25=\"http://schemas.openxmlformats.org/drawingml/2006/compatibility\" " +
"xmlns:ns26=\"http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas\" " +
"xmlns:ns24=\"http://schemas.openxmlformats.org/officeDocument/2006/bibliography\" " +
"xmlns:dsp=\"http://schemas.microsoft.com/office/drawing/2008/diagram\" " +
"xmlns:w10=\"urn:schemas-microsoft-com:office:word\" " +
"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:powerpoint\" " +
"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:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" " +
"xmlns:o=\"urn:schemas-microsoft-com:office:office\" " +
"xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\" " +
"xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" " +
"xmlns:ns13=\"urn:schemas-microsoft-com:office:excel\" " +
"xmlns:ns6=\"http://schemas.openxmlformats.org/schemaLibrary/2006/main\" " +
"xmlns:odq=\"http://opendope.org/questions\" " +
"xmlns:ns11=\"http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing\" " +
"xmlns:ns8=\"http://schemas.openxmlformats.org/drawingml/2006/chartDrawing\" " +
"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:odc=\"http://opendope.org/conditions\" " +
"w:rsidRPr=\"002655B2\"><w:rPr><w:sz w:val=\"20\"/><w:szCs w:val=\"20\"/></w:rPr></w:r>"
);
R uncheck4 = (R) XmlUtils.unmarshalString(
"<w:r " +
"xmlns:ns25=\"http://schemas.openxmlformats.org/drawingml/2006/compatibility\" " +
"xmlns:ns26=\"http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas\" " +
"xmlns:ns24=\"http://schemas.openxmlformats.org/officeDocument/2006/bibliography\" " +
"xmlns:dsp=\"http://schemas.microsoft.com/office/drawing/2008/diagram\" " +
"xmlns:w10=\"urn:schemas-microsoft-com:office:word\" " +
"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:powerpoint\" " +
"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:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" " +
"xmlns:o=\"urn:schemas-microsoft-com:office:office\" " +
"xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\" " +
"xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" " +
"xmlns:ns13=\"urn:schemas-microsoft-com:office:excel\" " +
"xmlns:ns6=\"http://schemas.openxmlformats.org/schemaLibrary/2006/main\" " +
"xmlns:odq=\"http://opendope.org/questions\" " +
"xmlns:ns11=\"http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing\" " +
"xmlns:ns8=\"http://schemas.openxmlformats.org/drawingml/2006/chartDrawing\" " +
"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:odc=\"http://opendope.org/conditions\" " +
"w:rsidRPr=\"002655B2\"><w:rPr><w:sz w:val=\"20\"/><w:szCs w:val=\"20\"/></w:rPr><w:fldChar w:fldCharType=\"end\"/></w:r>"
);
r.getContent().clear();
r.getContent().add(uncheck1);
r.getContent().add(uncheck2);
r.getContent().add(uncheck3);
r.getContent().add(uncheck4);
}
The problem I run into is when I try to do both of these on the same document. If i do the text field population part first, I get an exception: XPathExpressionException on the 2nd line of this:
- Code: Select all
String xpath = "//w:r[w:t[contains(text(),'${NEW_PROTOCOL}')]]";
List<Object> list = mainDocumentPart.getJAXBNodesViaXPath(xpath, false);
log.debug("list:" + list);
Exception caused by: TransformerException: Unable to evaluate expression using this context
If i do the checkbox population code first, for some reason the checkboxes don't appear.