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

问题描述:

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

I want to get information written in Textbox in an MS word document. I am using Apache POI to parse word document.

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

Currently I am iterating through all the Paragraph objects but this Paragraph list does not contain information from TextBox so I am missing this information in output.

例如

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 从文本框中提取信息吗?

Anyone knows how to extract information from text box using Apache POI?

这对我有用,

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

     }