Page 1 of 1

Replace Text in Document using unmarshallFromTemplate

PostPosted: Thu Dec 04, 2008 3:37 pm
by fiorenzo
Hi,
i want use my templates with some placeholder for dinamically replace.
I read this topic (http://dev.plutext.org/forums/viewtopic.php?f=6&t=6&sid=43f1d4b2a5fa55a3c6746ed0c72ecb96), but i found this solution a lot impractical.
I want test this solution:
Code: Select all
      //read template file
      WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
            .load(new java.io.File(templateFileWithPlaceHolder));
      MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
      Document wmlDocumentEl1 = (Document) documentPart.getJaxbElement();
      org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart
            .getJaxbElement();
      //xml --> string
      String xml = XmlUtils.marshaltoString(wmlDocumentEl, true);
      HashMap<String, String> mappings = new HashMap<String, String>();
      mappings.put("PROP1", "bla");
      mappings.put("PROP2", "bla bla");
      //valorize template
      Object obj = XmlUtils.unmarshallFromTemplate(xml, mappings);
      //change  JaxbElement
      documentPart.setJaxbElement(obj);
      SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
      //save  new documnt
      saver.save(newFile);

No errors... but the new file is identical to templateFileWithPlaceHolder.

Thank's in advance

Fiorenzo

Re: Replace Text in Document using unmarshallFromTemplate

PostPosted: Fri Dec 05, 2008 8:57 am
by fiorenzo
Hi,

little progress...

If i use:
Code: Select all
                MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
      Document wmlDocumentEl1 = (Document) documentPart.getJaxbElement();
      String xml = XmlUtils.marshaltoString(wmlDocumentEl1, true);
                java.util.HashMap<String, String> mappings = new HashMap<String, String>();
      mappings.put("_placeh_", "Flowers");
                Object obj = XmlUtils.unmarshallFromTemplate(xml, mappings);
   
                MainDocumentPart wordDocumentPart = new MainDocumentPart();
      // Put the content in the part
      wordDocumentPart.setJaxbElement(obj);
      // Add the main document part to the package relationships
      // (creating it if necessary)
      wordMLPackage.addTargetPart(wordDocumentPart);

      SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
      saver.save(file);

The new document contains my placeholder valorized..but it don't contains original relationships....

Should i perhaps copy the other parts and then add the new document?
Code: Select all
try {
         JAXBContext jc = Context.jc;
         Unmarshaller u = jc.createUnmarshaller();
         u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler());

         org.docx4j.wml.Package wmlPackageEl = (org.docx4j.wml.Package)u.unmarshal(
               new javax.xml.transform.stream.StreamSource(new FileInputStream(inputfilepath)));
         org.docx4j.wml.Styles wmlStyles = null;
         for (org.docx4j.wml.Package.Part p : wmlPackageEl.getPart() ) {
                  
            if (p.getXmlData().getStyles()!= null) {
               wmlStyles = p.getXmlData().getStyles();
            }   
         //ETC ETC
         }



Fiorenzo

Re: Replace Text in Document using unmarshallFromTemplate

PostPosted: Mon Dec 08, 2008 11:48 am
by jason
Hi, the approach you outlined in your first post should work.

http://dev.plutext.org/trac/docx4j/changeset/588 is an example, based on your code, which I have verified works.

There is no need to try what you are doing in the second post in this thread, but if you do replace the one Main Document Part with another, you will (as you have observed) lose the relationships attached to the first, unless you transfer those to your new main document part, with:

RelationshipsPart rp = oldmaindoc.getRelationshipsPart();
newmaindocpart.setRelationships(rp);

cheers

Jason

Re: Replace Text in Document using unmarshallFromTemplate

PostPosted: Tue Dec 09, 2008 5:07 pm
by fiorenzo
Hi Jason,

I had forgotten to include $ in the placeholder ---->>>> {placeholder} instead of ${placeholder}

!!!!!!
ops
!!!!!
Fiorenzo

Re: Replace Text in Document using unmarshallFromTemplate

PostPosted: Sat Jan 24, 2009 9:48 am
by twentyseven
Hello,

I used the http://dev.plutext.org/trac/docx4j/changeset/588 example with the docx given in example ; I have no problem.

Then I tried to create my own docx file abd the variables were note replaced.
After some investigation, I looked at the Strind that is passed to the replace method, and I found that the name of my variable was note in the same text portion :

<w:r w:rsidR="00BA2863">
<w:t>$
</w:t>
</w:r>
<w:r w:rsidR="00496984">
<w:t>{test}
</w:t>
</w:r>

I'm new with the docx format, so I don't understand why the text is separated in two portions (I didn't put different style...)

Thank's

Re: Replace Text in Document using unmarshallFromTemplate

PostPosted: Tue Jan 27, 2009 3:50 am
by jason
<w:r w:rsidR="00BA2863">
<w:t>$
</w:t>
</w:r>
<w:r w:rsidR="00496984">
<w:t>{test}
</w:t>
</w:r>


Word has split your text into 2 runs, so it can attach different rsid. See http://blogs.msdn.com/brian_jones/archive/2006/12/11/what-s-up-with-all-those-rsids.aspx for details.

You can edit your document (quite easily in Winzip or Package Explorer) to join up the 2 runs, like so:

Code: Select all
<w:r>
    <w:t>${test}</w:t>
</w:r>


before feeding your document into your Java code.

Alternatively, it is possible to turn off rsid's in Word, so it doesn't insert them in the first place.