views:

262

answers:

1

I am not able to update value of a custom property in a word document using java.

I have a word document which contains a custom property with value 'stack'. Using java I want to change that value to 'overflow'. I used two approaches.

1) Using Apache POI

I set the org.apache.poi.hpsf.CustomProperties in org.apache.poi.hpsf.DocumentSummaryInformation and written it to POIFSFileSystem.

It does updates the value in word's customproperties table. But doesn't update the value for that respective property in the document. After the document is opened, I need to manually refresh the document to get that value updated.

2) Parse the document char by char and use the field codes DC3, DC4 and NAK to identify the location of custom properties. Replace the existing value with the new value.

Now the generated document contains the new values. But the problem here is, if the length of the old value and new value is different the document gets corrupted. I made sure the logic is good enough.

Any help would be appreciated.

A: 

To update the approach I took to solve the issue:

Using Office 2007 (docx) document as a template became easier. It is nothing but a zip document. You can open it using your zip application (winzip/7zip) and you can find many xml files inside it. document.xml contains the content, styles.xml contains formatting information and so on.

At runtime, I unzipped the document and parsed the document.xml, then used dom and updated with dynamic content. Custom properties are available in a separate xml file.

For 2003 users, they have to prepare the template using word application and save the document as XML then provide it as input.

No use of apache-poi now.

Joseph Kulandai