Apache POI can not add CustomProperty to Doc

687 Views Asked by At

I'm trying to add some Custom Properties to an existing document:

HWPFDocument document = new HWPFDocument(new FileInputStream(sourceFile));
DocumentSummaryInformation docSumInf = document.getDocumentSummaryInformation();
CustomProperties customProperties = docSumInf.getCustomProperties();

CustomProperty singleProp = null;
//...
singleProp = new CustomProperty();
singleProp.setName(entry.getKey());
singleProp.setType(Variant.VT_LPWSTR);
singleProp.setValue((String) entry.getValue());
//..

customProperties.put(entry.getKey(), singleProp);
docSumInf.setCustomProperties(customProperties);
return document; 

However, the properties never make it to the file. I tried to

document.getDocumentSummaryInformation().getCustomProperties().putAll(customProperties);

I also tried

document.getDocumentSummaryInformation().getCustomProperties().put(entry.getKey(), singleProp);
System.out.println(document.getDocumentSummaryInformation().getCustomProperties().size() + " Elemente in Map");

in a loop. The printed size was allways one.

With the first attemp (docSumInf.setCustomProperties(customProperties);) I printed out customProperties before setting it to docSumInf. There where all new Properties I miss, as soon as I set them to the document summary.

I don't see what I am missing...

1

There are 1 best solutions below

6
On
entry.getKey() = null 

or entry.getKey() has common value for all CustomProperties in the map.

and because of that you have only one element in the map of CustomProperties.

You need to set non null values here

singleProp.setName(entry.getKey());

CustomProperty class represents custom properties in the document summary information stream. The difference to normal properties is that custom properties have an optional name. If the name is not null it will be maintained in the section's dictionary.