【问题标题】:How to get text from textbox of MS word document using Apache POI?如何使用 Apache POI 从 MS Word 文档的文本框中获取文本?
【发布时间】:2011-07-24 09:17:20
【问题描述】:

我想在 MS Word 文档中获取用 Textbox 编写的信息。我正在使用 Apache POI 来解析 word 文档。

目前我正在遍历所有段落对象,但此段落列表不包含来自 TextBox 的信息,因此我在输出中缺少此信息。

例如

paragraph in plain text

**<some information in text box>**

one more paragraph in plain text

我要提取的内容:

<para>paragraph in plain text</para>

<text_box>some information in text box</text_box>

<para>one more paragraph in plain text</para>

我目前得到的:

纯文本段落

一个纯文本段落

有人知道如何使用 Apache POI 从文本框中提取信息吗?

【问题讨论】:

  • @plutext,从 doc 格式开始,但以后需要对 docx 和 rtf 做同样的事情。
  • 您可以考虑使用 JODConverter + LibreOffice 将所有三种格式转换为 docx,然后使用 POI(或 docx4j)从 docx 中提取文本框内容。这样您就不必担心二进制格式或解析 rtf。
  • @plutext,非常感谢.. 我会研究 JODConverter。我希望它是免费的。
  • @Shekhar 您知道如何从 .docx 文档的文本框中提取文本吗?如果您这样做了,我们随时欢迎您分享该信息。 ;)

标签: ms-word document apache-poi


【解决方案1】:

这对我有用,

    private void printContentsOfTextBox(XWPFParagraph paragraph) {

        XmlObject[] textBoxObjects =  paragraph.getCTP().selectPath("
            declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' 
            declare namespace wps='http://schemas.microsoft.com/office/word/2010/wordprocessingShape' 
            declare namespace v='urn:schemas-microsoft-com:vml'
            .//*/wps:txbx/w:txbxContent | .//*/v:textbox/w:txbxContent");

        for (int i =0; i < textBoxObjects.length; i++) {
            XWPFParagraph embeddedPara = null;
            try {
            XmlObject[] paraObjects = textBoxObjects[i].
                selectChildren(
                new QName("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "p"));

            for (int j=0; j<paraObjects.length; j++) {
                embeddedPara = new XWPFParagraph(
                    CTP.Factory.parse(paraObjects[j].xmlText()), paragraph.getBody());
                //Here you have your paragraph; 
                System.out.println(embeddedPara.getText());
            } 

            } catch (XmlException e) {
            //handle
            }
        }

     } 

【讨论】:

  • 更新:结果并非所有文本框都在给定示例的模式中。这是另一个, textBoxObjects.addAll(Arrays.asList(paragraph.getCTP().selectPath("declare namespace w='schemas.openxmlformats.org/wordprocessingml/2006/main' declare namespace v='urn:schemas-microsoft-com:vml' .//*/v :textbox/w:txbxContent")));.我没有找到 OOXML 模式定义提供的文本框模式的完整列表,如果有人有请分享。
【解决方案2】:

为了从 crgrep 的 Word .doc 和 .docx 文件中提取所有出现的文本,我使用了 Apache Tika 源作为正确使用 Apache POI API 的参考。如果您想直接使用 POI 而不依赖于 Tika,这很有用。

对于 Word .docx 文件,看看这个 Tika 类:

org.apache.tika.parser.microsoft.ooxml.XWPFWordExtractorDecorator

如果您忽略 XHTMLContentHandler 和格式化代码,您可以看到如何使用 POI 正确导航 XWPFDocument。 对于 .doc 文件,此类很有帮助:

org.apache.tika.parser.microsoft.WordExtractor

都来自tika-parsers-1.x.jar。通过 maven 依赖项访问 Tika 代码的一种简单方法是将 Tika 临时添加到您的 pom.xml 中,例如

<dependency>
    <groupId>org.apache.tika</groupId>
    <artifactId>tika-parsers</artifactId>
    <version>1.7</version>
</dependency>

让您的 IDE 解析附加的源代码并进入上面的类。

【讨论】:

    【解决方案3】:

    如果您想从 docx 文件的文本框中获取文本(使用 POI 3.10-FINAL),这里是示例代码:

    FileInputStream fileInputStream = new FileInputStream(inputFile);
    XWPFDocument document = new XWPFDocument(OPCPackage.open(fileInputStream)); 
    for (XWPFParagraph xwpfParagraph : document.getParagraphs()) {
         String text = xwpfParagraph.getParagraphText(); //here is where you receive text from textbox
    }
    

    或者你可以遍历每个 XWPFParagraph 中的 XWPFRun 并调用 toString() 方法。结果一样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-18
      相关资源
      最近更新 更多