B4J Question How to read a word document in B4J

Marco Gioia

Member
Licensed User
Hi everyone,

Where can I find a complete example on how to read a word document (docx)?

In don't need to write it..

Thanks in advance
 

MarkusR

Well-Known Member
Licensed User
Longtime User
i believe its a zip and inside it have xml files.
rename it to .docx.zip and you can open it at pc (same for xlsx excel)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Where can I find a complete example on how to read a word document (docx)?
i have´nt seen a library for this.
You need to search the forum for any library available.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
You can read it using jPOI library and a bit of inline code and javaobject library.
B4X:
…
 asJO(Me).RunMethod("readDocxFile",Array("C:/temp/test.docx"))
End Sub
Sub asJO(o As JavaObject)As JavaObject
 Return o
End Sub
#if java
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
public static void readDocxFile(String fName) {
    try {
        File file = new File(fName);
        FileInputStream fis = new FileInputStream(file.getAbsolutePath());
        XWPFDocument document = new XWPFDocument(fis);
        List<XWPFParagraph> paragraphs = document.getParagraphs();

        for (XWPFParagraph para : paragraphs) {
            System.out.println(para.getText());
        }
        fis.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
#End If
 
Upvote 0

Marco Gioia

Member
Licensed User
You can read it using jPOI library and a bit of inline code and javaobject library.
B4X:
…
 asJO(Me).RunMethod("readDocxFile",Array("C:/temp/test.docx"))
End Sub
Sub asJO(o As JavaObject)As JavaObject
 Return o
End Sub
#if java
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
public static void readDocxFile(String fName) {
    try {
        File file = new File(fName);
        FileInputStream fis = new FileInputStream(file.getAbsolutePath());
        XWPFDocument document = new XWPFDocument(fis);
        List<XWPFParagraph> paragraphs = document.getParagraphs();

        for (XWPFParagraph para : paragraphs) {
            System.out.println(para.getText());
        }
        fis.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
#End If

Ok, it seems to be working fine.
What next?
I mean, to read the text, what should I do?
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
You can change the inline code to return the text if you want.
B4X:
public static String readDocxFile(String fName) {
String rep = "";   
try {
        File file = new File(fName);
        FileInputStream fis = new FileInputStream(file.getAbsolutePath());
        XWPFDocument document = new XWPFDocument(fis);
        List<XWPFParagraph> paragraphs = document.getParagraphs();

        for (XWPFParagraph para : paragraphs) {
            //System.out.println(para.getText());
            rep += para.getText()+"\n";
        }
        fis.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rep;
}
Then call it with
B4X:
Dim docText as String = asJO(Me).RunMethod("readDocxFile",array("path to the file"))
docText will contain the text of the document.
 
Last edited:
Upvote 0

Marco Gioia

Member
Licensed User
You are right!

B4X:
        for (XWPFParagraph para : paragraphs) {
            System.out.println(para.getText());

I thought it was only to open it :D

Thanks
 
Upvote 0
Top