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: 666
  • Printer.zip
    14.2 KB · Views: 371
Last edited:

stevel05

Expert
Licensed User
Longtime User
Just tried it with Java 11.0.1 and it works for me from the modules. Have you compiled it to a library? Try compiling it again.
 

Peter Simpson

Expert
Licensed User
Longtime User
The original demo works with Java 8 but not Java 11 for me. Must some difference in the JavaFX implementation. Under Java 11 it doesn't show the Print Dialog so the selected printer is always null.

Thank you for that.
But I've not had Java 8 installed in a long long long time.

At the bottom of my first post, I even types and I quote from May this year, that was using OpenJDK 11.
"I can't show you the print outs, but trust me when I say that they look great. They were seriously easy to create using the B4J built-in designer and the Print JavaFX8 library wrapped by @stevel05, thank you ???" .


I need to look into this in more detail, it's completely baffled me, as I know for a fact it was also working in OpenJDK 14 only 2 weeks ago.
 

stevel05

Expert
Licensed User
Longtime User
Although I just tried it using a compiled library and it works too. I have the java 11 version that is provided on the B4J download page.
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
You are correct Andrew, I just wondered if Peter was using a compiled version from an earlier java version and may need to update it.
 

agraham

Expert
Licensed User
Longtime User
I can report that the original demo works without compiling the library under OpenJDK 14 as well as Oracle Java 8 but not under OpenJDK 11. Both OpenJDK versions were downloaded from here.

Under OpenJDK 11 line 25 in AppStart PJ.ShowPrintDialog(Null) fails to show the Print Dialog and so with no printer selected it doesn't work. I don't know enough to delve further at this moment.
 

Peter Simpson

Expert
Licensed User
Longtime User
Thank you both @agraham and @stevel05 for your valuable feedback, but I'm completely lost.
The last time I definitely saw the printouts working was around 2 weeks ago when I send a PDF to a client for them to okay the printout, I was using 14, but now nothing to do with any B4J project is printing, I'm just getting the same error over and over again, even with the original example in the first post.

I'm thinking about removing all JAVA from my development laptop and installing them again. Basically nothing has changed in the last 2 weeks except that I've purchased a licence for NetLimiter Pro 4 (I e been using the trial for months) and I've had Windows update, I've decided that it has nothing to do with B4J, but sadly I have absolutely no idea what is going on.
 

AHilton

Active Member
Licensed User
Longtime User
Surely, it wouldn't have to do with the Windows 10 update from last week(?) that caused printing problems with USB printers and the "Print to PDF" feature?
 

stevel05

Expert
Licensed User
Longtime User
Well done Peter.

My PC is set to let Windows manage the default printer, so unless I uncheck that, I don't have the option to specifically set a default printer. In case it happens again, it may be best to call Printer_Static.GetDefaultPrinter check that is is initialized, and then call PrinterJob_Static.CreatePrinterJob2 and pass the printer.

It's frustrating that an OS update can break these things, even without Windows managing the default printer, it doesn't seem possible to remove the Default printer selection altogether, so I'm not sure how it could have got into that state otherwise.

Steve
 

Peter Simpson

Expert
Licensed User
Longtime User
I DELETED MY PREVIOUS POST AND ADDED THIS POST INSTEAD, STEVE KINDLY REPLIED THIS POST BUT IT IS NOW UNDERNEATH STEVE'S POST.

Surely, it wouldn't have to do with the Windows 10 update from last week(?) that caused printing problems with USB printers and the "Print to PDF" feature?
Bingo :cool:

@AHilton don't take this the wrong way, but when I first read your reply I though to myself absolutely no way, you're shooting into thin air. After trying my 2nd laptop and finding that I couldn't get my B4J code working on that either, I decided to install B4J and OpenJDK 14 onto my partners laptop, I almost died when the Print FX8 example worked first time of asking. So I got to thinking, what exactly has changed. Both my laptops had major Windows updates last week, but my partners laptop has not been turned on in about 2 weeks. I uninstalled all Java versions, disabled anti-virus and firewall, deleted the B4J .ini file the lot but still nothing worked, so I reinstalled Java and re-enabled everything again, then your improbable answer popped into my mind.

Issue resolved:
My default printer setting on my development laptop is always set to Bullzip PDF, on my 2nd laptop the default printer setting is always set to Microsoft PDF. When I checked both my laptops, oddly there were no default printers selected, (there were no GREEN CHECKED PRINTERS). I always have a default printer set and it's always Bullzip PDF (Laptop 1) and Microsoft PDF (Laptop 2). I set both the laptops back to their default printers and low and behold all my B4J projects started printing again.

I've just reinstalled Java 8, 10.2, 11 and 14, all my B4J projects are printing with all Java versions (I'm just about to uninstall 8). I called my client and he contacted staff what were still in the office, they too had no default printer settings, they had a Windows update which they installed yesterday evening. I had incorrectly presumed that their issues started with the updated B4J project that I sent them earlier today. It was pure coincidental that the application wasn't printing after they installed my update, in any case their printing issues would still have happened.

I had to reassigned the default printer below (again), now my B4J projects are working again @AHilton, thank you.
1592511331700.png


I do have one question though @AHilton. How did you know (where did you read) about PDF issues after the latest Windows 10 update, I've not received any news about printer issues in my Windows issues feeds???

By the way, thank you @agraham and @stevel05 for your time and help, it's really appreciated.



Cheers...

#WindowsUpdateStrikeAgain
 
Last edited:

Peter Simpson

Expert
Licensed User
Longtime User
In case it happens again, it may be best to call Printer_Static.GetDefaultPrinter check that is is initialized, and then call PrinterJob_Static.CreatePrinterJob2 and pass the printer.

Hello Steve,
Thank you for the suggestion, I will do that tomorrow (in all my projects, I'll send all my clients updates) and both myself and this particular client have had a stressful day. My client has a couple of hundred invoices and orders to send out today, and I got a bit stressed out trying to fix the issue (thank you Microsoft). As I mentioned in one of my previous posts above, your printing library has never let me down so this issue came as a bit of a shock to me :confused:

Anyway, everything is back to normal and my client is happy again, thus so am I :)
 

AHilton

Active Member
Licensed User
Longtime User
I'm glad that was it. When you said "and I've had Windows update" in message #128, that reminded me of what I've been reading (worrying) about. I'm not sure where, exactly, I read about Microsofts' latest update problem concerning this. One of my IT guys bugs me with these kinds of things all the time and he put in front of my face earlier this week from one of the IT news or blog sites, I suppose. I was following along with your troubles because it's one of those fears that I have with our own software because we produce a lot of PDF's for reports, etc., too. I use them quite a bit in B4J as well so I was a bit worried about when it was going to hit me.
 

le_toubib

Active Member
Licensed User
Longtime User
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

Depends on: JavaFX8, JavaObject

If you just want the libs, then download JFX8PrintLibs, unzip it and copy the jar and xml to your addl libs directory. Or you can download the source and compile it yourself.

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)

Enjoy
hello , thanks for the great library ,
trying to print a webview , and it works fine except that the arabic letters are printed in reverse ??? the html content is set to charset = UTF-8 ... is there a way to fix that?
 

Pelky

Active Member
Licensed User
Longtime User
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

Depends on: JavaFX8, JavaObject

If you just want the libs, then download JFX8PrintLibs, unzip it and copy the jar and xml to your addl libs directory. Or you can download the source and compile it yourself.

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/

Enjoy
this is wonderful however i am not as experienced as many of the other developers and am having a problem. I dont understand this root pane thing or the need for it.
I want to be able to design a layout using the designer and then populate it - once populated i want to ask the user if he/she wants a hard copy - if the answer is in the affirmative i want to be able to queue this out to a printer (selected) from available printers.
is there a way to do this?
Sorry to be a pain the fundamentals
 

Peter Simpson

Expert
Licensed User
Longtime User
this is wonderful however i am not as experienced as many of the other developers and am having a problem.

PLEASE NOTE:
The link below is an example using Scene Builder, DO NOT USE SCENE BUILDER. You can get the exact same results using the built-in designer in B4J ;)


Enjoy...
 
Top