Page 1 of 1

Word doesn't update fields edited by docx4j

PostPosted: Thu Jul 16, 2009 1:05 pm
by hugopais
Hi All,

I'm using docx4j to edit some docx properties and it all works fine except that when i open the document using Word 2007 i have to manualy update de fields.

Is there any way to prevent this behaviour?

Thanks in advance
Hugo Pais

Re: Word doesn't update fields edited by docx4j

PostPosted: Thu Jul 16, 2009 7:51 pm
by jason
hugopais wrote:when i open the document using Word 2007 i have to manualy update de fields.


That's right; Word doesn't do it for you.

I think you have 2 options:

Option 1 - Work out for yourself what needs to go in the fields, and insert the relevant values with docx4j.

I appreciate that depending on what fields you are using, option 1 might be impractical.

Option 2 - Write a little macro which does this automatically. Google "word field update automatically". You can have docx4j inject the macro into your document at the time it is created. I have done this, and it works nicely (I can post the code). Whether this will work for you depends on your users macro security settings in Word Options > Trust Center >
Trust Center Settings > Macro Settings

Re: Word doesn't update fields edited by docx4j

PostPosted: Thu Jul 16, 2009 8:19 pm
by hugopais
Thanks for the info i kind of suspect it.... just wishfull thinking....

If you could please post that code i would grately appreciate it.

Many thanks
Hugo Pais

Re: Word doesn't update fields edited by docx4j

PostPosted: Fri Jul 17, 2009 7:31 am
by jason
Code: Select all

package org.docx4j.samples;

import javax.xml.bind.JAXBContext;
import org.docx4j.openpackaging.io.SaveToZipFile;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;

public class MacroEnable {
   
   public static JAXBContext context = org.docx4j.jaxb.Context.jc;

   /**
    * @param args
    */
   public static void main(String[] args) throws Exception {

      String inputfilepath = "/home/dev/workspace/docx4all/sample-docs/earth.docx";      
      String outputfilepath = "/home/dev/docm.docm";      
            
      // Open a document from the file system
      // 1. Load the Package
      WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
      
      MainDocumentPart wordDocumentPart = wordMLPackage.getMainDocumentPart();
      
      // Get vbaProject.bin, and attach it to wordDocumentPart
      java.io.InputStream is = new java.io.FileInputStream("/home/dev/vbaProject.bin" );            
      org.docx4j.openpackaging.parts.WordprocessingML.VbaProjectBinaryPart vbaProject
         = new org.docx4j.openpackaging.parts.WordprocessingML.VbaProjectBinaryPart();
      vbaProject.setBinaryData(is);
      wordDocumentPart.addTargetPart(vbaProject);
      
      // Get /word/vbaData.xml, and attach it to vbaProject
      org.docx4j.openpackaging.parts.WordprocessingML.VbaDataPart vbaData
         = new org.docx4j.openpackaging.parts.WordprocessingML.VbaDataPart(); 
      java.io.InputStream is2 = new java.io.FileInputStream("/home/dev/vbaData.xml" );
      vbaData.setDocument( is2 );
      
      vbaProject.addTargetPart( vbaData);
         
      // Change the Word document's content type!
      wordDocumentPart.setContentType( new org.docx4j.openpackaging.contenttype.ContentType(
            org.docx4j.openpackaging.contenttype.ContentTypes.WORDPROCESSINGML_DOCUMENT_MACROENABLED ) );
      // .. but that's a dumb setter right now :(
      // it doesn't actually make the change in ContentTypeManager.  So do that:
      org.docx4j.openpackaging.contenttype.ContentTypeManager ctm
         = wordMLPackage.getContentTypeManager();
      org.docx4j.openpackaging.parts.PartName partName
         = wordDocumentPart.getPartName();
      
      ctm.removeContentType( partName  );
      ctm.addOverrideContentType( new java.net.URI("/word/document.xml"),
            org.docx4j.openpackaging.contenttype.ContentTypes.WORDPROCESSINGML_DOCUMENT_MACROENABLED);
   
            
      // Save it      
      SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
      saver.save(outputfilepath);
   }
   

}



To create vbaProject.bin and vbaData.xml , create a document in Word containing your desired macro, and save it. Those 2 parts should be in the resulting .docx (unzip it and save them).

Re: Word doesn't update fields edited by docx4j

PostPosted: Tue Apr 08, 2014 4:32 pm
by PoojaGe
Hi Jason,

I am using word 2010, and i have fields in footer with document property filename.
I dont intend to use Macro, Is it possible to automatically update the fields using some docx4j API.
Will FieldUpdater in docx4j serve this purpose?

Re: Word doesn't update fields edited by docx4j

PostPosted: Tue Apr 08, 2014 6:33 pm
by jason
That ought to update DOCPROPERTY fields, yes. Did you try it?

Re: Word doesn't update fields edited by docx4j

PostPosted: Fri Apr 25, 2014 12:18 am
by PoojaGe
Hi Jason,
I did try that but it doesn't seem to work :(
wonder what's wrong

Regards,
Pooja

Re: Word doesn't update fields edited by docx4j

PostPosted: Fri Apr 25, 2014 8:27 am
by jason
We can only guess, unless you provide a sample docx and your code!

Re: Word doesn't update fields edited by docx4j

PostPosted: Fri Apr 25, 2014 5:34 pm
by PoojaGe
Hi Jason,

PFB the code:


File fInputfile = null;
WordprocessingMLPackage wordML = null;
fInputfile = new File("D:\\Input.docx");
File fOutputfile = new File("D:\\Output.docx");

try {
wordML = WordprocessingMLPackage.load(fInputfile);
wordML.save(fOutputfile);
FieldUpdater f1 = new FieldUpdater(wordML);
f1.update(true);

} catch (Docx4JException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Thanks for your help,
Pooja

Re: Word doesn't update fields edited by docx4j

PostPosted: Fri Apr 25, 2014 11:09 pm
by jason
In your Input.docx, footer2.xml contains:

Code: Select all
<w:instrText xml:space="preserve"> FILENAME   \* MERGEFORMAT </w:instrText>


but none of the parts contain any DOCPROPERTY fields.

The FILENAME field is different.

Re: Word doesn't update fields edited by docx4j

PostPosted: Mon May 05, 2014 9:17 pm
by PoojaGe
Hi Jason,

Thanks I got what u said.
But i am looking at something to be updated dynamically for example everytime a document is generated i gave it a new file name like v1,v2,.. and so on.
Is there some API in docx4j to update fileName property automatically on opening the document?? I know there is a check box in MS Word which says update fields before printing but i want the similar thing to happen on opening the document.
Else i am thinking of using page number docproperty which shows the no of pages in the generated document.

PS: I don't intend to use Macros.

Many Thanks,
Pooja