B4J Question OpenDocX and Word Textbox objects

Discussion in 'B4J Questions' started by kohle, Jun 7, 2019.

Thread Status:
Not open for further replies.
  1. kohle

    kohle Active Member Licensed 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 ...
    Code:
    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))
       
    ....
     

    Attached Files:

  2. Erel

    Erel Administrator Staff Member Licensed User

    I wasn't able to read text from text boxes however you can use single cell tables instead:

    Code:
    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
    [​IMG]
     
  3. kohle

    kohle Active Member Licensed User

    Last edited: Jun 10, 2019
  4. Erel

    Erel Administrator Staff Member Licensed User

    I wasn't able to find a way to extract shapes from the paragraph or document. If you find reasonable Java code that does it (without directly parsing the XML) I can help you port it.
     
  5. kohle

    kohle Active Member Licensed 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.
    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

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

     }
     
  6. Erel

    Erel Administrator Staff Member Licensed 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:
    Code:
    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.
     
  7. JOTHA

    JOTHA Well-Known Member Licensed User

    I'm also interested in getting a solution for inserting text into a word document with placeholders like <VORNAME> or $VORNAME ...
     
  8. Erel

    Erel Administrator Staff Member Licensed User

Thread Status:
Not open for further replies.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice