B4J Question [XLUtils] Word how to set document page size

Num3

Active Member
Licensed User
Longtime User
Hello all!

I've been exploring word document creation using XLUtils, but the documents are set to Letter, how can i change the default size to A4 or any other size i need?

I also came across this thread on stackoverflow, but i don't know how to call pageSize.setW(BigInteger.valueOf(xxxxx));

Thanks in advance.

(sorry i posted in tutorials by mistake)
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Private Sub SetPageSize(doc As WordDocument, Width As Long, Height As Long)
    Dim document As JavaObject = doc.XWPFDocument.RunMethod("getDocument", Null)
    Dim body As JavaObject = document.RunMethod("getBody", Null)
    If body.RunMethod("isSetSectPr", Null).As(Boolean) = False Then
        body.RunMethod("addNewSectPr", Null)
    End If
    Dim section As JavaObject = body.RunMethod("getSectPr", Null)
    If section.RunMethod("isSetPgSz", Null).As(Boolean) = False Then
        section.RunMethod("addNewPgSz", Null)
    End If
    Dim pageSize As JavaObject = section.RunMethod("getPgSz", Null)
    Dim BigInteger As JavaObject
    BigInteger.InitializeStatic("java.math.BigInteger")
    pageSize.RunMethod("setW", Array(BigInteger.RunMethod("valueOf", Array(Width * 20))))
    pageSize.RunMethod("setH", Array(BigInteger.RunMethod("valueOf", Array(Height * 20))))
End Sub

Common values: https://stackoverflow.com/a/24500643

B4X:
SetPageSize(doc, 595, 842) 'A4
 
Upvote 0

Num3

Active Member
Licensed User
Longtime User
This programming language is amazing, it was so simple to convert the code :D . Another day, another thing learned!

I have encountered a new problem (refered on the stack overflow), when executing the sub throws the following error :

B4X:
WARNING: package com.sun.javafx.embed.swing.oldimpl not in javafx.swing
Waiting for debugger to connect...
Program started.
Cannot get methods of class: org.openxmlformats.schemas.wordprocessingml.x2006.main.impl.CTDocument1Impl, disabling cache.
Error occurred on line: 170 (Main)
java.lang.NullPointerException: Cannot read the array length because "params" is null
    at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(JavaObject.java:340)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:120)
    at b4j.example.main._setpagesize(main.java:327)
    at b4j.example.main$ResumableSub_gerar_Click.resume(main.java:233)
    at b4j.example.main._gerar_click(main.java:197)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:577)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:577)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.BA$1.run(BA.java:236)
    at javafx.graphics@18.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics@18.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics@18.0.1/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics@18.0.1/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics@18.0.1/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    at java.base/java.lang.Thread.run(Thread.java:833)

I know "org.openxmlformats.schemas.wordprocessingml.x2006.main.impl.CTDocument1Impl" must inported before hand, because from another example in the forums i picked this up:

B4X:
#if java
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
#End If

I've pasted the above code inside the sub, but no effect, because the sub is pure B4J...

Can someone give another push on this?
Thank you all.
 
Upvote 0
Top