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

3

3

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 information from text box using Apache POI?

Offensive answered 28/3, 2011 at 10:39 Comment(4)
@plutext, To start with doc format but later need to do same for docx and for rtf also.Offensive
You could consider using JODConverter + LibreOffice to convert all three formats to docx, and then extract the textbox contents from the docx using POI (or docx4j). That way you don't need to worry about the binary format, or parsing rtf.Pyrostat
@plutext, Thanks a lot.. I will look into JODConverter. I hope its free.Offensive
@Offensive Did you find out how to extract the text from a textbox in .docx document? If you did, you are always welcome to share that info. ;)Airplane
B
7

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

     } 
Bluegrass answered 16/9, 2014 at 19:49 Comment(1)
Update: Turns out not all textboxes are within the schema in the given example. Here's another, 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")));. I didn't find complete list of textbox schemas provided by OOXML schema definition, if anybody has please share.Bluegrass
D
2

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.

Darkness answered 14/3, 2015 at 6:47 Comment(0)
W
0

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.

Wristband answered 23/4, 2014 at 9:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.