【问题标题】:How to modify description tag of a docx document如何修改docx文档的描述标签
【发布时间】:2013-07-19 07:09:39
【问题描述】:

我正在使用 docx4j 来读取 word 文档的内容。

core.xml 有一个description 标记,我想在我正在阅读的文档中对其进行修改。

最好的方法是什么?我是否必须阅读文档的全部内容并使用 docx4j 创建一个新文档并更改 description 标签,或者有​​没有办法只更改 description 标签而不修改和/或阅读->复制文件?

【问题讨论】:

    标签: java docx4j


    【解决方案1】:

    请参阅第 64 行的示例 DocProps.java,了解如何获取核心道具部分。

    然后是这样的:

        JAXBElement<SimpleLiteral> desc = coreProps.getDescription();
        SimpleLiteral literal = XmlUtils.unwrap(desc);
        List<String> contents = literal.getContent();
    

    然后修改该列表。与 JAXB 一样,它是一个实时列表,因此您将立即对文档的 in-mem 表示进行更改。

    或者您可以创建一个新的 JAXBElement desc2,然后是 coreProps.setDescription(desc2)。这就是你对没有 dc:description 的 docx 所做的:

        org.docx4j.docProps.core.dc.elements.ObjectFactory dcFactory = new org.docx4j.docProps.core.dc.elements.ObjectFactory();
        SimpleLiteral literal = dcFactory.createSimpleLiteral();
        coreProps.setDescription(dcFactory.createDescription(literal));
        List<String> contents = literal.getContent();
        // populate contents ...
    

    然后保存 docx。上面链接的示例就是这样做的。

    【讨论】:

    • 此方法适用于在 core.xml 中具有 description 标记的文档。但是,有些文档 (DOCX) 没有description 标签。在这些情况下,我会在literal.getContent() 上获得 NPE。有没有办法在不存在的地方设置描述标签?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    • 1970-01-01
    • 2016-06-15
    • 2018-04-04
    相关资源
    最近更新 更多