B4J Question OpenDocX and Word Textbox objects

Status
Not open for further replies.

kohle

Active Member
Licensed User
Longtime User
I am using Erel´s example for opening a word docx file (in his case a excel file)
and replacing some text .

My problem is that I dont know how to read the contents of textbox objects.
(see screenshot)


Code I use looks like this ...
B4X:
  Dim paragraphs As List = doc.RunMethod("getParagraphs", Null)
       For Each p As JavaObject In paragraphs
           Dim runs As List = p.RunMethod("getRuns", Null)
           If runs.IsInitialized Then
               For Each r As JavaObject In runs
                   Dim text As String = r.RunMethod("getText", Array(0))
   
....
 

Attachments

  • 07-06-_2019_21-02-40.png
    07-06-_2019_21-02-40.png
    33.6 KB · Views: 332

Erel

B4X founder
Staff member
Licensed User
Longtime User
I wasn't able to read text from text boxes however you can use single cell tables instead:

B4X:
Sub AppStart (Args() As String)
   Dim doc As JavaObject = OpenDocx("C:\Users\H\Downloads\1.docx", "")
   Dim tables As List = doc.RunMethod("getTables", Null)
   For Each table As JavaObject In tables
       Dim row As JavaObject = table.RunMethod("getRow", Array(0))
       Dim cell As JavaObject = row.RunMethod("getCell", Array(0))
       Dim paragraphs As List = cell.RunMethod("getParagraphs", Null)
       For Each p As JavaObject In paragraphs
           Dim runs As List = p.RunMethod("getRuns", Null)
           If runs.IsInitialized Then
               For Each r As JavaObject In runs
                   Dim text As String = r.RunMethod("getText", Array(0))
                   If text <> Null Then
                       Log(text)
                   End If
               Next
           End If
       Next
   Next
End Sub

WINWORD_a9GwkjM8so.png
 
Upvote 0

kohle

Active Member
Licensed User
Longtime User
Last edited:
Upvote 0

kohle

Active Member
Licensed User
Longtime User
What I find is :
https://stackoverflow.com/questions...-textbox-of-ms-word-document-using-apache-poi

It shows "solutions" depending on the version of poi.
If you want to get text from textbox in docx file (using POI 3.10-FINAL) here is sample code:
B4X:
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.


or

B4X:
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' .//*/wps:txbx/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
        }
    }

 }
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The first one will not work. I've tried it.

The second one doesn't look like valid Java code. Java doesn't support multiline strings.
Try this:
B4X:
Dim TextBoxObjects() As Object = p.RunMethodJO("getCTP", Null).RunMethod("selectPath", Array($"
       declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'
       declare namespace wps='http://schemas.microsoft.com/office/word/2010/wordprocessingShape' .//*/wps:txbx/w:txbxContent")
Check the value of TextBoxObjects with the debugger.
 
Upvote 0

JOTHA

Well-Known Member
Licensed User
Longtime User
I'm also interested in getting a solution for inserting text into a word document with placeholders like <VORNAME> or $VORNAME ...
 
Upvote 0
Status
Not open for further replies.
Top