How to modify description tag of a docx document

♀尐吖头ヾ 提交于 2019-12-02 16:37:09

问题


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.

What is a best way to do this? Will I have to read the entire content of the document and create a new document using docx4j and change the description tag or is there a way to just change the description tag without modifying and/or reading->copying the content of the document?


回答1:


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.



来源:https://stackoverflow.com/questions/17755563/how-to-modify-description-tag-of-a-docx-document

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!