So far we've been successfully replacing variables in the document.xml file of docx Word files.
We used to do that by inserting variables like ${varName}
directly in our Word templates, and then running a piece of code to change the variables*
We're going to migrate to a rails app, and we are going to use form controls and Word 2007 content control Toolkit to databind these form controls to custom XML parts.
If possible i'd like to make it compatible with our old app. So basically I need to do the same thing to "customXML/items2.xml" (replacing variables) than what I'm doing to document.xml.
However, these custom XML parts are not treated as similar objects by docx4j and I don't really understand how I could access these. I thought of the following :
CustomXmlDataStoragePart customPart = wordMLPackage.getParts().get("/customXml/item2.xml");
String customXML = XmlUtils.marshaltoString(customPart, true);
Object obj2 = XmlUtils.unmarshallFromTemplate(customXML, mappings);
documentPart.setJaxbElement((org.docx4j.openpackaging.parts.CustomXmlDataStoragePart) obj2);
But play Throws an error
error: method get in class Parts cannot be applied to given types;
What am I doing/thinking wrong ?
By the way, I know it is now possible to use Form controls with docx4j, but I'm using an old version of play, and for practical reason I don't want to rewrite all the code. I just want to reapply the mapping I already applied to document.xml to items2.xml.
Working code we used to change the variables in document.xml:
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart.getJaxbElement();
String xml = XmlUtils.marshaltoString(wmlDocumentEl, true);
HashMap<String, String> mappings = getMapping();
Object obj = XmlUtils.unmarshallFromTemplate(xml, mappings);
documentPart.setJaxbElement((org.docx4j.wml.Document) obj);
// Some code to deal with the footers
SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
saver.save(outputfilepath);
No comments:
Post a Comment