使用apache poi更新word 2010中的自定义属性

时间:2014-03-04 09:49:46

标签: java ms-word apache-poi

我需要在设置新值后使用Apache Poi更新自定义属性。

我有为自定义属性设置新值的方法:

  InputStream fs = new FileInputStream("D://test.docx");        
  XWPFDocument document = new XWPFDocument(OPCPackage.open(fs));

    POIXMLProperties props = document.getProperties();
    CustomProperties cp = props.getCustomProperties();
    if (cp != null) {
        List<CTProperty> ctProperties = cp.getUnderlyingProperties()
                .getPropertyList();
        for (CTProperty ctp : ctProperties) {
            System.out.println(ctp.getName());
            if (ctp.getName().equals("Test")) {
                ctp.setLpwstr("Test");
            }

        }
    }

    document.write(new FileOutputStream(new File("D://test.docx")));
    fs.close();

但是当我打开文档时,属性值已经过时了。我必须手动更新它们。

有一种方法可以使用Apache Poi自动更新属性值吗? 谢谢!

P.S。抱歉我的英语不好。

1 个答案:

答案 0 :(得分:2)

我有同样的问题,但我解决了问题:

document.enforceUpdateFields();

当用户打开文档时,将打开确认信息,以确定更新字段。