Page 1 of 1

Mapping Template to Word 2007 file

PostPosted: Wed Sep 30, 2009 5:41 pm
by td16
How can a docx file be generated based on an specific Word template (.dot). My guess is that this information is stored in docProps/app.xml? Also how can properties be set in docProps/custom.xml? Any example would help. thanks!

Re: Mapping Template to Word 2007 file

PostPosted: Thu Oct 01, 2009 3:12 am
by jason
td16 wrote:How can a docx file be generated based on an specific Word template (.dot). My guess is that this information is stored in docProps/app.xml?


I haven't done any work with .dotx files, but ... I just saved a document as a template (.dotx) in Word 2007, and opened it in package explorer.

It has the same parts as a normal .docx; app.xml has a Properties/Template entry, but all docs generated by Word have that.

The difference (now opening it using WinZip) is that in [Content_Types].xml, we have this entry for document.xml.

Code: Select all
  <Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml" />


So, what you need to do is open the .dotx file, manipulate the resulting package as you wish using docx4j, and then, before you save it, change that entry in [Content_Types].xml. Something like:

Code: Select all
wordMLPackage.getContentTypeManager().addOverrideContentType(partUri, contentType)


td16 wrote:Also how can properties be set in docProps/custom.xml? Any example would help. thanks!


Assuming there is already org.docx4j.openpackaging.parts.DocPropsCustomPart docPropsCustomPart, you just:

Code: Select all
docPropsCustomPart.setProperty(propName, propValue)


If it doesn't exist already, you create the part and add it to the document.

Here is an example of doing that, and populating it from an existing file (exception handling omitted):

Code: Select all
public static void injectDocPropsCustomPart(WordprocessingMLPackage wordMLPackage) {
      
         org.docx4j.openpackaging.parts.DocPropsCustomPart docPropsCustomPart = new org.docx4j.openpackaging.parts.DocPropsCustomPart();         
         java.io.InputStream is = new java.io.FileInputStream("/tmp/custompart.xml" );         
         docPropsCustomPart.unmarshal(is);         
         wordMLPackage.addTargetPart(docPropsCustomPart);
   }


Please report back to the forum on how you go.

cheers .. Jason

Re: Mapping Template to Word 2007 file

PostPosted: Fri Oct 02, 2009 11:57 pm
by td16
thanks Jason. This helps. Instead of using a dotx, I used an empty docx file based on the dotx file I need as a template. I read the file (from my jar file), add information into it and write out a separate output file.

The original template docx file already has a footer in it, how can I manipulate data or add data into the same footer?

Re: Mapping Template to Word 2007 file

PostPosted: Tue Oct 06, 2009 1:01 am
by jason
td16 wrote:how can I manipulate data or add data into the same footer?


You just have to get the footer part, and then edit its contents.

org.docx4j.model.HeaderFooterPolicy makes it easy to get the part, and WordprocessingMLPackage has a method getHeaderFooterPolicy().

Re: Mapping Template to Word 2007 file

PostPosted: Wed Oct 07, 2009 4:14 pm
by td16
I tried getting the footer using the headerfooterpolicy and modifying it (I just add a para to it). For some reason MS Word complains that the document is corrupt but after the automatic fix, it shows up properly. Any ideas? tx

Re: Mapping Template to Word 2007 file

PostPosted: Thu Oct 08, 2009 12:14 am
by jason
Sounds like it might be a bug.

The first thing to check is that the rel id's in the document match the rels in /word/_rels/document.xml.rels

If you are on Windows, you could try this tool: http://blogs.msdn.com/brian_jones/archi ... ation.aspx

If it turns out docx4j is doing something wrong, please post a test case. If you can't find what is wrong with your document, please post a copy (preferably saved as Flat OPC from within docx4j - see org/docx4j/samples/ExportInPackageFormat.java), and i will take a look.

Re: Mapping Template to Word 2007 file

PostPosted: Mon Oct 12, 2009 11:34 pm
by td16
thanks. I used the Windows .Net code through the link you specified to debug. The problem is not with the footer but with the document property.

Code: Select all
Error 1
Description: Attribute 'name' should have unique value. Its current value 'Title' duplicates with others.
Path: /op:Properties[1]/op:property[7]
Part: /docProps/custom.xml


The template document already has a property 'Title' defined and I am trying to update the property using

Code: Select all
docPropsCustomPart.setProperty(propName, propValue)


This creates another property named 'Title' with a different pid and thus the clash. How can I update an existing property?

Re: Mapping Template to Word 2007 file

PostPosted: Tue Oct 13, 2009 12:26 am
by jason
td16 wrote:Description: Attribute 'name' should have unique value. Its current value 'Title' duplicates with others.


Revision 932 should fix this. Its not tested, so please let report back whether it works.

I've uploaded http://dev.plutext.org/docx4j/docx4j-ni ... 091013.jar which incorporates the change.

For anybody that finds this topic in Google, note that <cp:coreProperties> includes <dc:title>, so you wouldn't ordinarily use a custom property to store the document's title.

Re: Mapping Template to Word 2007 file

PostPosted: Tue Oct 13, 2009 2:35 am
by td16
nope. still the same error. The old property is updated with new pid and value but it is repeated twice.

The original property is

Code: Select all
<prop:property name="Title" pid="3" fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}">
  <vt:lpwstr>Some Name</vt:lpwstr>
</prop:property>


When it is updated, property shows up twice

Code: Select all
<prop:property name="Title" pid="8" fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}">
  <vt:lpwstr>New Name</vt:lpwstr>
  </prop:property>
<prop:property name="Title" pid="8" fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}">
  <vt:lpwstr>New Name</vt:lpwstr>
  </prop:property>

Re: Mapping Template to Word 2007 file

PostPosted: Tue Oct 13, 2009 5:19 am
by jason
td16 wrote:nope. still the same error. The old property is updated with new pid and value but it is repeated twice.


Oops, sorry about that .. goes to show it always pays to test!

Revision 933; I've updated the nightly linked above.

Again, please advise whether it works.

Re: Mapping Template to Word 2007 file

PostPosted: Tue Oct 13, 2009 6:17 am
by td16
works perfectly.
thanks
Tushneem.