1

我正在尝试将一些自定义属性添加到现有文档中:

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; 

但是,这些属性永远不会进入文件。我尝试过了

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

我也试过

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

在一个循环中。打印的尺寸总是一。

第一次尝试 (docSumInf.setCustomProperties(customProperties);) 在将其设置为 docSumInf 之前,我打印出了 customProperties。一旦我将它们设置为文档摘要,我就会错过所有新属性。

我没有看到我错过了什么......

4

1 回答 1

1
entry.getKey() = null 

entry.getKey()对地图中的所有 CustomProperties 具有共同值。

因此,您在 CustomProperties 的地图中只有一个元素。

您需要在此处设置非空值

singleProp.setName(entry.getKey());

CustomProperty类表示文档摘要信息流中的自定义属性。与普通属性的区别在于自定义属性具有可选名称。如果名称不为空,它将保留在该部分的字典中。

于 2017-06-27T12:24:55.820 回答