Page 1 of 1

custom properties

PostPosted: Thu Jun 12, 2014 11:58 pm
by eladp
Hi,

I have a docx document with custom properties and in the first run the properties stays with the font i wrote them
the second time i try to overwrite the property the font changes to Calibri
why doe's it happen?

first time the input file is temp.docx and the output file is result.docx the result.docx font set to Arial, the second time i change the input to the created result.docx and the output file to result2.docx, in the output2.docx the font changes to Calibri

here is my code:

Code: Select all
package com.doc;

import java.util.HashMap;
import java.util.Map;

import org.docx4j.XmlUtils;
import org.docx4j.docProps.custom.Properties.Property;
import org.docx4j.model.fields.FieldUpdater;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;

public class Docx4jMain {

   private static final String PATH = "C:\\Users\\v0018540\\Downloads\\temp\\";
   private static final String BREAK_LINE = "#br#";
   private static final String BR = "</w:t><w:br/><w:t>";
   private static Map<String, String> attrMap = new HashMap<String, String>();

   @SuppressWarnings("deprecation")
   private static void injectXml(WordprocessingMLPackage wordMLPackage) throws Exception
   {
      MainDocumentPart part = wordMLPackage.getMainDocumentPart();
      String xml = XmlUtils.marshaltoString(part.getJaxbElement(), true);
      xml = xml.replaceAll(BREAK_LINE, BR);
      Object obj = XmlUtils.unmarshalString(xml);
      part.setJaxbElement((org.docx4j.wml.Document) obj);
   }

   public static void updatePropertiesValues(Map<String, String> propertiesMap) throws Exception
   {
      WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(PATH + "temp.docx"));
      org.docx4j.docProps.custom.Properties.Property newProp = null;
      org.docx4j.openpackaging.parts.DocPropsCustomPart docPropsCustomPart = wordMLPackage.getDocPropsCustomPart();
      org.docx4j.docProps.custom.Properties customProps = (org.docx4j.docProps.custom.Properties) docPropsCustomPart.getJaxbElement();

      for (Property prop : customProps.getProperty())
      {
         if (propertiesMap.containsKey(prop.getName()))
         {
            String lpwstrVal = propertiesMap.get(prop.getName());

            try
            {
               newProp = prop;
               newProp.setLpwstr(lpwstrVal);               
            }
            catch (Exception e)
            {
               e.printStackTrace();
            }
         }
      }

      FieldUpdater fu = new FieldUpdater(wordMLPackage);
      fu.update(true);
      injectXml(wordMLPackage);
      wordMLPackage.save(new java.io.File(PATH + "result.docx"));
   }

   public static void main(String[] args)
   {
      attrMap.put("DocTempField", "Test");
      try
      {      
         updatePropertiesValues(attrMap);
         System.out.println("Doc created");
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
   }

}


the second time i run the program i can see that some fonts tags are missing compared to the first xml run.

thank you in advance.

still no one can help???
please help!!!

Re: custom properties

PostPosted: Thu Aug 08, 2019 11:49 pm
by tobo
Hi eladp,
did you solve the problem with font replacement? I have exactly the same problem...