B4J Library B4j Print JavaFX8

Here is a B4j library written in B4j to access the full Printer modules provided with JavaFX8. Full source code is available.

At it's simplest, you can print a node using:
B4X:
    Dim P As Printer = Printer_Static.GetDefaultPrinter
    Dim PJ As PrinterJob = PrinterJob_Static.CreatePrinterJob2(P)
    PJ.PrintPage(lblTest)
    PJ.EndJob

Or with dialogs:

B4X:
    Dim PJ As PrinterJob = PrinterJob_Static.CreatePrinterJob
    PJ.ShowPageSetupDialog(Null)
    PJ.ShowPrintDialog(Null)
    PJ.PrintPage(MainForm.RootPane)
    PJ.EndJob

Code to scale a view to print on a single page.
Scale output:
Sub ScaleOutput(P As Printer,N As Node) As Node
    Dim PL As PageLayout = P.GetDefaultPageLayout
    Dim ScaleX,ScaleY As Double
    Dim NJO As JavaObject = N
    Dim JO As JavaObject = N
    ScaleX = PL.GetPrintableWidth / JO.RunMethodJO("getBoundsInParent",Null).RunMethod("getWidth",Null)
    ScaleY = PL.GetPrintableHeight / JO.RunMethodJO("getBoundsInParent",Null).RunMethod("getHeight",Null)
    Dim SJO As JavaObject
    SJO.InitializeNewInstance("javafx.scene.transform.Scale",Array(ScaleX,ScaleY))
    NJO.RunMethodJO("getTransforms",Null).RunMethod("add",Array(SJO))
    Return NJO
End Sub

Usage:
    'Print with dialogs
    Dim PJ As PrinterJob = PrinterJob_Static.CreatePrinterJob
    If PJ.ShowPageSetupDialog(Null) Then
        If PJ.ShowPrintDialog(Null) Then
'            PJ.PrintPage(MainForm.RootPane)
            PJ.PrintPage(ScaleOutput(PJ.GetPrinter,MainForm.RootPane))
            PJ.EndJob
        Else
            PJ.CancelJob
        End If
    Else
        PJ.CancelJob
    End If

Reset node size after scaling:
'where N would be MainForm.Rootpane in the above example
N.As(JavaObject).Runmethod("getTransforms",Null).as(List).Clear

Depends on: JavaFX8, JavaObject

Documentation (apart from that in the library) is available here: http://docs.oracle.com/javase/8/javafx/api/index.html?javafx/print/Printer.html
Click on javafx.print in the All Classes Packages frame to see all the relevant classes.

I've ignored Enums where strings are acceptable for simplicity's sake.

V0.6 fix improperly formed XMLmessage displayed in IDE help (post #6)
V0.7 added #RaisesSynchronousEvents to PrinterJob module as described here : https://www.b4x.com/android/forum/t...ble-getting-jfx8print-dialogs-to-work.133964/
V 0.8 Released as a B4xLib Enums now return string values instead of ENum objects.

Full code is in Printer.zip if you just want to look at the project without unzipping the B4xlib
Enjoy
 

Attachments

  • jFX8Print.b4xlib
    11.1 KB · Views: 433
  • Printer.zip
    14.2 KB · Views: 159
Last edited:

DieterR

Member
Licensed User
Longtime User
Thank You very much Erel!
"use Java 8" was the essential tip. I did not respect that "jFX8" is different from "jFX".:oops:
After I installed the latest Java Versions my program works perfect.:)
 

le_toubib

Active Member
Licensed User
Longtime User
that s owsom
i tried it with a webview1 to print an html file, but the result did not fit into an A4 page , HTML font may be too large , i donno, i set the paper layout with A4 , portrait , and margins of 5px.
how can i set the webview to fit into the A4 page ?
 

stevel05

Expert
Licensed User
Longtime User
No, it would have to be rendered to a webview, the FX routines will print the contents of a View.
 

tdocs2

Well-Known Member
Licensed User
Longtime User
Greetings.

I am able to print the first page of a multipage tableview. How do I have to show the tableview (e.g., Erel's Student Tableview) so that I am able to print several pages?

Thank you in advance for your replies.

Sandy
 

stevel05

Expert
Licensed User
Longtime User
It'll print whatever is displayed in the view, so you'll need to scroll the table and print it again for each page.
 

micro

Well-Known Member
Licensed User
Longtime User
He stevel05
why with your example view this error?
B4X:
B4J version: 3.61
Parsing code.    (0.01s)
Compiling code.    Error
Error compiling program.
Error description: Array expected.
Occurred on line: 23
PJ.ShowPageSetupDialog(Null)
Word: (
Thanks
 

stevel05

Expert
Licensed User
Longtime User
Hi Micro,

I just downloaded and ran it as is and I don't see that problem.

Steve
 

micro

Well-Known Member
Licensed User
Longtime User
If work with module (bas) is ok, but if you load the lib and remove the module declared twice, appears the error.
 

stevel05

Expert
Licensed User
Longtime User
The lib is compiled from the modules, you don't need both.
 

micro

Well-Known Member
Licensed User
Longtime User
In effect if i use the lib appears the error, while if i disables the loading lib and insert modules work fine.
Thanks
 

stevel05

Expert
Licensed User
Longtime User
Yes, you can use the library in a different project without having to load all of the related modules.
 

boten

Active Member
Licensed User
Longtime User
Tried to print a text area to PDF (using a PDF printer).
Text area has white background, but the pdf shows the text over a grayish rectangle the size of the textarea.

How can I print just the text without the bounding rectangle?

print is done via:
B4X:
Sub Button1_Action
   Dim PJ As PrinterJob = PrinterJob_Static.CreatePrinterJob
  PJ.ShowPageSetupDialog(Null)
  PJ.ShowPrintDialog(Null)
   PJ.PrintPage(TextArea1)
  PJ.EndJob
End Sub
 

stevel05

Expert
Licensed User
Longtime User
The Javafx8 print prints an entire view not just the text, you will need to change the background color of the view to suit.
 

boten

Active Member
Licensed User
Longtime User
The Javafx8 print prints an entire view not just the text, you will need to change the background color of the view to suit.
As I mentioned, the text area has white background

In the code I have:
B4X:
s=TextArea1.Style
TextArea1.Style=s & "-fx-background: #FFFFFF"  
Log(s)

and logged the style. Log says:
B4X:
-fx-font-family:Georgia;-fx-font-size:22.00;-fx-border-color:#FFFFFF;
-fx-border-width:0.00;-fx-background: #FFFFFF
 

boten

Active Member
Licensed User
Longtime User
In contrast to TextArea - a Label with white background prints just fine
 

boten

Active Member
Licensed User
Longtime User
........... (See attached project).

Very interesting Steve.
Since I need to change the text's font, fontsize and alignment, and since a Label is easier to change, I decided to stick with a label and copy the textarea's text to Label.text
 
Top