如何使用 Apache POI 从 MS Word 文档的文本框中获取文本?

She*_*har 3 document ms-word apache-poi

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

目前,我正在迭代所有段落对象,但此段落列表不包含 TextBox 中的信息,因此我在输出中丢失了此信息。

例如

paragraph in plain text

**<some information in text box>**

one more paragraph in plain text
Run Code Online (Sandbox Code Playgroud)

我想提取什么:

<para>paragraph in plain text</para>

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

<para>one more paragraph in plain text</para>
Run Code Online (Sandbox Code Playgroud)

我目前得到的:

纯文本段落

纯文本中的另一段

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

Chi*_*may 6

这对我有用,

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

     } 
Run Code Online (Sandbox Code Playgroud)

  • 更新:事实证明并非所有文本框都在给定示例中的架构内。这是另一个,textBoxObjects.addAll(Arrays.asList(paragraph.getCTP().selectPath("声明命名空间 w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' 声明命名空间 v='urn:schemas- microsoft-com:vml' .//*/v:textbox/w:txbxContent")));。我没有找到 OOXML 模式定义提供的文本框模式的完整列表,如果有人有请分享。 (3认同)