Page 1 of 1

Cast to document on mapping

PostPosted: Tue Aug 20, 2013 2:07 am
by p101
Hello,

I'm working on trying to learn docx4j and right now I'm trying to deal with mappings to replace placeholders in a document (attached). I'm working off this example: http://www.docx4java.org/svn/docx4j/tru ... plate.java

The code isn't mapping correctly and just turns out the document with the same items as the template, just without the ${}. So, for example, the template has ${HED} and the result will be HED.

I am able to add paragraphs of text without issue.

One thing I noticed and didn't see in the documentation is Java is wanting to cast the object to a document while changing the JaxbElement. So instead of documentPart.setJaxbElement(obj); it becomes documentPart.setJaxbElement((Document) obj); to eliminate the error and compile. I'm guessing this is the issue, I'm just not sure how to work around it.

My code is:

Code: Select all
package tutorial;

// imports

import java.util.HashMap;
import java.util.List;

import javax.xml.bind.JAXBException;
import javax.xml.bind.JAXBContext;

import org.docx4j.XmlUtils;
import org.docx4j.openpackaging.exceptions.Docx4JException;
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 Test {
   
    public static JAXBContext context = org.docx4j.jaxb.Context.jc;
   
   public static void main (String args[]) throws Exception {
      
      // load document
      
      WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File("C:/TEMP/template.docx"));
      
      // Get the main document part from the template already loaded
      
      MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
      
       org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart.getJaxbElement();
      
      // Instantiate the class
      ClassSample wg = new ClassSample();
      wg.callDB();
      
      // Get the number of rows in the array from the Counter class
      Counter counter = new Counter();
      
      int row_count = counter.getCount("world_globe");
      
      // Edit the main document part from the template document
      
      //xml --> string
      String xml = XmlUtils.marshaltoString(wmlDocumentEl, true);
                      
      HashMap<String, String> mappings = new HashMap<String, String>();
                      
      mappings.put("HED", "headline");
      mappings.put("SUB", "subhead");
                      
      //valorize template
       Object obj = XmlUtils.unmarshallFromTemplate(xml, mappings);
                      
      //change  JaxbElement
      documentPart.setJaxbElement((Document) obj);
      
      // Loop through the data and add paragraphs to the document
      for (int i = 0; i < row_count; i++){   
         
         String output = wg.getDataLine(i);
         
         if (output != null) {
         
            documentPart.addParagraphOfText(output);
         
         }
      }
      
      boolean save = true;
      
      if (save) {
         SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
         saver.save("C:/Users/hjkh/Desktop/Output.docx");
      }
      
   }
   
   public interface ContentAccessor {
      
      public List<Object> getContent();
      
   }


}


The error in console:

3316 [main] WARN org.docx4j.XmlUtils - Invalid key '</w:t></w:r><w:proofErr w:type="spellStart"/><w:r w:rsidR="002B4468"><w:rPr><w:rFonts w:hAnsi="Albertus MT" w:ascii="Albertus MT"/><w:b/><w:sz w:val="72"/></w:rPr><w:t>hed</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r><w:rPr><w:rFonts w:hAnsi="Albertus MT" w:ascii="Albertus MT"/><w:b/><w:sz w:val="72"/></w:rPr><w:t>' or key not mapped to a value
3316 [main] WARN org.docx4j.XmlUtils - Invalid key '</w:t></w:r><w:r w:rsidR="002B4468"><w:rPr><w:rFonts w:hAnsi="Arial Narrow" w:ascii="Arial Narrow"/><w:sz w:val="44"/></w:rPr><w:t>sub</w:t></w:r><w:r><w:rPr><w:rFonts w:hAnsi="Arial Narrow" w:ascii="Arial Narrow"/><w:sz w:val="44"/></w:rPr><w:t>' or key not mapped to a value

(Note: I am calling some other classes that gather data to write into paragraphs (just to figure this out). They're not the issue as the mapping is not working. I guess one exception would be if the mapping is getting overridden by .addParagraphOfText.)

Any help you have would be great! I'm attaching the template.

Thanks!

Re: Cast to document on mapping

PostPosted: Tue Aug 20, 2013 5:41 am
by p101
I ended up tracking it down to this issue: docx-java-f6/umarshallfromtemplate-doesn-t-work-t897.html#p2936

The cast made no difference.

I had to create the template off one revision of Word. If I saved, then adjusted a font size, then saved again, that document doesn't work. Does anyone know how to configure Word to not make that an issue or is saving a fresh document the best solution overall?