How to modify description tag of a docx document

前端 未结 1 604
遇见更好的自我
遇见更好的自我 2021-01-26 13:08

I\'m using docx4j to read contents of a word document.

The core.xml has a description tag which I would like to modify in the documents I\'m reading.

相关标签:
1条回答
  • 2021-01-26 13:35

    See the sample DocProps.java at line 64 for how to fetch the core props part.

    Then its something like:

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

    Then modify that list. As is typical with JAXB, its a live list, so your changes will be made immediately to the in-mem representation of the document.

    Or you could create a new JAXBElement<SimpleLiteral> desc2, then coreProps.setDescription(desc2). That's what you'd do for a docx which doesn't have a dc:description already:

        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 ...
    

    Then save the docx. The sample linked above does that.

    0 讨论(0)
提交回复
热议问题