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?

Shekhar
  • 11,438
  • 36
  • 130
  • 186
  • @plutext, To start with doc format but later need to do same for docx and for rtf also. – Shekhar Mar 31 '11 at 10:44
  • 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. – JasonPlutext Mar 31 '11 at 12:07
  • @plutext, Thanks a lot.. I will look into JODConverter. I hope its free. – Shekhar Apr 01 '11 at 05:34
  • 1
    @Shekhar 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. ;) – SuperRetro Mar 28 '14 at 08:28

3 Answers3

6

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

     } 
meiMing
  • 68
  • 5
Chinmay
  • 731
  • 1
  • 8
  • 19
  • 3
    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='http://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. – Chinmay Sep 18 '14 at 17:15
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.

Craig
  • 1,383
  • 12
  • 9
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.

Maciek Murawski
  • 414
  • 4
  • 15