1

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...

J.K
  • 25
  • 7

1 Answers1

1
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.

Mike Adamenko
  • 2,944
  • 1
  • 15
  • 28
  • The entry I use is also from an HashMap: `for(Entry entry : dataMap.entrySet()) { ....}`. I also tried assigning unique Long values as Keys, that didn't work either. So I am shure the keys are not null.. – J.K Jun 27 '17 at 12:50
  • 1
    make sure in the debugger that entry.getKey() is not null – Mike Adamenko Jun 27 '17 at 12:51
  • I can't debug this code easily because of the enviroment. I printed the value in Log Files, so I confirmed they are not null... – J.K Jun 27 '17 at 12:53
  • do you do this `customProperties.put(entry.getKey(), singleProp);` in loop ? – Mike Adamenko Jun 27 '17 at 13:02
  • Yes. To be shure, this is not the mistake, I also added an out-of-the-loop-rubbish property. This is also missing. `singleProp = new CustomProperty();` happens in the loop for every property that does not exist earlier. – J.K Jun 27 '17 at 13:10
  • see if you have different entry.getKey() values in every loop cycles – Mike Adamenko Jun 27 '17 at 13:22