【问题标题】:How to get values from Ms word merge field using Docx4j Java如何使用 Docx4j Java 从 Ms 单词合并字段中获取值
【发布时间】:2014-01-12 14:39:48
【问题描述】:

我正在尝试在 java 中使用 Docx4j 从合并字段中检索值。目前,我正在使用以下方式检索 word 文档的所有内容:

WordprocessingMLPackage newWordMLPackage = WordprocessingMLPackage
    .load(new java.io.File("C:/Users/admin/Desktop/test" + i + ".docx"));
MainDocumentPart documentPart = newWordMLPackage.getMainDocumentPart();                 
System.out.println(documentPart.getContent());

这将返回 word 文档中的内容列表。我目前得到的是

MERGEFIELD lastName \* MERGEFORMAT himura

我想要的是从合并字段“lastName”中获取值'himura'。我怎样才能做到这一点?
谢谢

【问题讨论】:

    标签: java docx4j


    【解决方案1】:

    您可以使用 xpath 来实现 - 请参阅方法 documentPart.getJAXBNodesViaXPath(xpath, false);

    我有类似的问题(想用我自己的内容替换 MergeField)。经过长时间的研究,我写了一个可以做到的方法:

    private void replaceTextWithElement(MainDocumentPart mainDocumentPart, String textToReplace, Collection<Object> newElements) throws JAXBException, Docx4JException {
            final String xpath = "//w:r[w:instrText[contains(text(),'MERGEFIELD') and contains(text(),'" + textToReplace + "')]]";
            final List<Object> foundNodes = mainDocumentPart.getJAXBNodesViaXPath(xpath, false);
            if (isEmpty(foundNodes)) {
                throw new RuntimeException("Cannot find textToReplace: \"" + textToReplace + "\" in document, skipping replacement.");
            }
    
            final R r = (R)foundNodes.get(0);
            final P parent = (P)r.getParent();
            final int index = mainDocumentPart.getContent().indexOf(parent);
    
            mainDocumentPart.getContent().remove(parent);
            if (newElements != null) {
                mainDocumentPart.getContent().addAll(index, newElements);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2014-01-09
      • 2014-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-14
      • 2012-06-20
      相关资源
      最近更新 更多