B4J Question jPOI for Tables in Word

Patent

Member
Licensed User
Longtime User
Dear Community,

have a Question about missing depencies, see the little Example.
Want to add a new Grid in a Table in a Word XDoc with jPOI, based on Erels
https://www.b4x.com/android/forum/threads/jpoi-to-the-rescue.104082/#content.

I copied all the .jars correctly. (All other things are working, except the CT-Table XML things.)
Tried also to reference and add the poi-ooxml-schemas-4.1.1.jar and XMLBeans.jar "manually", but the same error.
Any ideas?
Greets
Patent
B4X:
Sub Process_Globals
    Private fx As JFX
    
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    test
End Sub


Sub test
    Dim docX As JavaObject = CreateNewDocx
    docX.RunMethod("createTable",Array(2,2))
    
    
    Dim bodies As List = docX.RunMethod("getBodyElements", Null)
    If bodies.IsInitialized Then
        For Each bodyelement As JavaObject In bodies
            Dim elementType As String=bodyelement.RunMethod("getElementType",Null)
            
            Select elementType
                Case "PARAGRAPH"
                    Dim runs As List = bodyelement.RunMethod("getRuns", Null)           
                    If runs.IsInitialized Then
                        For Each run As JavaObject In runs
                            Dim zeilenText As String = run.RunMethod("getText", Array(0))
                            If zeilenText <> Null And zeilenText<>"null" And zeilenText<>" " Then
                                Log("Paragraph: "&zeilenText)
                            End If
                        Next
                    End If
                            
                Case "TABLE"
                    Dim te As JavaObject=bodyelement.RunMethodJO("getCTTbl",Null).RunMethod("addNewTblGrid",Null)
            End Select
        Next
    End If
End Sub


Sub CreateNewDocx() As JavaObject
    Dim document As JavaObject
    document.InitializeNewInstance("org.apache.poi.xwpf.usermodel.XWPFDocument", Null)
    Return document
End Sub
Waiting for debugger to connect...
Program started.
Cannot get methods of class: org.openxmlformats.schemas.wordprocessingml.x2006.main.impl.CTTblImpl, disabling cache.
Error occurred on line: 34 (Main)
java.lang.NullPointerException
at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(JavaObject.java:340)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:120)
at b4j.example.main._test(main.java:153)
at b4j.example.main._appstart(main.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
at b4j.example.main.start(main.java:38)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
 

Daestrum

Expert
Licensed User
Longtime User
Can you post the error message as it will make It easier to work out what is missing.
Ignore that I just spotted the spoiler at the bottom.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Patent

Member
Licensed User
Longtime User
Changed the Java Part, because 1st Version was just working with OpenOffice, this one works also with MS-Word!:rolleyes:

I did it with Erel's Hint:
Here is the snip for others who want do some similar: (here: set the Columns Width for a Table in docx Documents)
Requires jPOI Lib

Greets



B4X:
Sub Process_Globals
    Private fx As JFX
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    GenerateTable
End Sub


Sub GenerateTable
    Dim docX As JavaObject
    docX.InitializeNewInstance("org.apache.poi.xwpf.usermodel.XWPFDocument", Null)
    docX.RunMethod("createTable",Array(2,2))
   
    Dim tables As List = docX.RunMethod("getTables", Null)
    If tables.IsInitialized Then
        For Each table As JavaObject In tables
           
            table.RunMethod("setWidthType",Array("DXA"))    'set to 1/20 of a point
            'bodyelement.RunMethod("setWidth",Array(7000))    'whole table
   
            Dim columnwidth(2) As Int
            columnwidth(0)=2200
            columnwidth(1)=3800
            Dim nativeMe As JavaObject=Me
            nativeMe.RunMethod("setTableColumnsWidth", Array(table,columnwidth))

        Next
    End If
   
    Dim out As OutputStream = File.OpenOutput("C:\Work", "table.docx", False)
    docX.RunMethod("write", Array(out))
    out.Close
End Sub



#If java

import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGrid;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import java.math.BigInteger;

    public static void setTableColumnsWidth(XWPFTable table, int... widths) throws Exception {
        CTTblGrid grid = table.getCTTbl().addNewTblGrid();
        int col=0;
        for (int w : widths) {
            grid.addNewGridCol().setW(BigInteger.valueOf(w));
            
            CTTblWidth tblWidth = table.getRow(0).getCell(col).getCTTc().addNewTcPr().addNewTcW();
              tblWidth.setW(BigInteger.valueOf(w));
            
            col++;
          }
    }
#End If
 
Last edited:
Upvote 0
Top