首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Apache POI无法将CustomProperty添加到文档

Apache POI无法将CustomProperty添加到文档
EN

Stack Overflow用户
提问于 2017-06-27 19:35:32
回答 1查看 365关注 0票数 1

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

代码语言:javascript
复制
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; 

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

代码语言:javascript
复制
document.getDocumentSummaryInformation().getCustomProperties().putAll(customProperties);

我也试过了

代码语言:javascript
复制
document.getDocumentSummaryInformation().getCustomProperties().put(entry.getKey(), singleProp);
System.out.println(document.getDocumentSummaryInformation().getCustomProperties().size() + " Elemente in Map");

在循环中。印刷的尺寸总是一号。

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

我不知道我错过了什么..。

EN

回答 1

Stack Overflow用户

发布于 2017-06-27 20:24:56

代码语言:javascript
复制
entry.getKey() = null 

或者,对于映射中的所有CustomProperties,entry.getKey()都具有公共值。

正因为如此,您在CustomProperties的映射中只有一个元素。

您需要在此处设置非空值

代码语言:javascript
复制
singleProp.setName(entry.getKey());

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44779291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档