开发者

How to get text from textbox of MS word document using Apache POI?

开发者 https://www.devze.com 2023-02-20 10:28 出处:网络
I want to get information written in Textbox in an MS word document. I am using Apache POI to parse word document.

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

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.

e.g.

paragraph in plain text

**<some information in text box>**

one more paragraph in plain text

what i want to extract :

<para>paragraph in plain text</para>

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

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

what I am getting currently :

paragraph in plain text

one more paragraph in plain text

Anyone knows how to extract i开发者_Python百科nformation from text box using Apache POI?


This worked for me,

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

     } 


To extract all occurrences of text from Word .doc and .docx files for crgrep I used the Apache Tika source as a reference of how the Apache POI APIs should be correctly used. This is useful if you want to use POI directly and not depend on Tika.

For Word .docx files, take a look at this Tika class:

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

if you ignore XHTMLContentHandler and formatting code you can see how to navigate a XWPFDocument correctly using POI. For .doc files this class is helpful:

org.apache.tika.parser.microsoft.WordExtractor

both from the tika-parsers-1.x.jar. An easy way to access the Tika code through your maven dependencies is add Tika temporarily to your pom.xml such as

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

let your IDE resolve attached source and step into the classes above.


If you want to get text from textbox in docx file (using POI 3.10-FINAL) here is sample code:

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
}

Or you can iterate over each XWPFRun in XWPFParagraph and invoke toString() method. Same result.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号