我正在尝试向现有文档添加一些自定义属性:
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。只要我将它们设置为文档摘要,我就会错过所有新属性的位置。
我不知道我错过了什么..。
发布于 2017-06-27 20:24:56
entry.getKey() = null 或者,对于映射中的所有CustomProperties,entry.getKey()都具有公共值。
正因为如此,您在CustomProperties的映射中只有一个元素。
您需要在此处设置非空值
singleProp.setName(entry.getKey());CustomProperty类表示文档摘要信息流中的自定义属性。与普通属性的不同之处在于,自定义属性具有可选名称。如果名称不为空,它将保留在节的字典中。
https://stackoverflow.com/questions/44779291
复制相似问题