B4J Library B4j Print JavaFX8

stevel05

Expert
Licensed 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
 

Attachments

Last edited:

swissmade

Active Member
Licensed User
Hi,
I'm using this library and it is working very well but I can not see how I set the Page-layout.
I like to have the layout like
Top 0
Left 0
I need this for a Bar-code Printer.
Maybe I don't see it :(any help from somebody.
Many thanks for help.
 

stevel05

Expert
Licensed User
Printer.CreatePageLayout allows setting Margins as long as the selected printer supports them. If it's a requirement of the printer, the perhaps Printer.GetDefaultPageLayout will already have them set.
 

stevel05

Expert
Licensed User
I've updated the files, please download and try again the improperly formatted XML issue should be fixed. I don't have a printer that can support 0 margins, you would also have to choose the appropriate page size, so can't test code to post. It shouldn't be difficult though.

You can test it using the dialogs to see if it works properly.

If you still get problems, let me know.
 

swissmade

Active Member
Licensed User
I don't have a printer that can support 0 margins,
Marges don't have to be 0.
I only need to know how to give the Pagelayout to the Printer.
I think I do something wrong.
See some code.
B4X:
    Dim PP As Paper
    PP.Initialize()
    Dim PC As Printer
    PC.Initialize()
    Dim PL As PageLayout
    PL.Initialize()
    PP = Paper_static.A6
    PC = Printer_Static.GetDefaultPrinter

PL = PC.CreatePageLayout2(PP, PageOrientation_Static.PORTRAIT, "HARDWARE_MINIMUM")

    Dim PJ As PrinterJob
    'Print with dialogs
    PJ = PrinterJob_Static.CreatePrinterJob2(PC) 'CreatePrinterJob
 

swissmade

Active Member
Licensed User
How are you then printing the page?
I think I have seen it:)
B4X:
    PJ.PrintPage(MainForm.RootPane)
    PJ.EndJob
'Must be
    PJ.PrintPage2(PL, MainForm.RootPane)
    PJ.EndJob
Let you know if it is working.
Marge must be at least 1 and not zero.
 

swissmade

Active Member
Licensed User
Small Questions.
How can I create my one Paper size??
I test this with Foxit Reader to create a pdf.
 

stevel05

Expert
Licensed User
The Constants in Paper_static are declared for convenience, you can get all of the papers that the printer says it supports using this code:

B4X:
    Dim P As Printer = Printer_Static.GetDefaultPrinter
   
    For Each PP As JavaObject In P.GetPrinterAttributes.GetSupportedPapers
        Dim PPP As Paper
        PPP.SetObject(PP)
        Log(PPP.ToString)
    Next
There may be a paper that you want listed.

There is a similar question on stack overflow here: http://stackoverflow.com/questions/19796091/javafx-print-custom-paper-size which suggests it's possible. I have to go out now, but if you don't have any luck converting it, I'll take a look when I get back. I'm not sure it will work, but may be worth a look if you need it.
 

swissmade

Active Member
Licensed User
Solution to Set your one Papersize
B4X:
 Dim r As Reflector
  r.Target = PP
  Log(r.GetField("width"))
    r.SetField2("width", 55)
    r.SetField2("height", 64)
    PPP.SetObject(PP)
Result a small Paper layout and Size. The Printer I test is an pdf Printer Foxit.
Maybe a option to put this in your library.
This only works when you are using.
B4X:
PC.GetPrinterAttributes.GetSupportedPapers 'Return List
'And Not with
PC.GetPrinterAttributes.GetDefaultPaper 'Return Paper
Solution Get the first Printer from SupportedPapers and change the values. Like
Width, Height and Name
 
Last edited:

Mark Zraik

Member
Licensed User
Hi Steve05,

Am I wrong in assuming (Hate to do that) that we should open your project in B4J, then compile it to a library and then use it in or own project?

I realize the answer might be obvious, but I like to clarify first. I'm gonna try exactly that, and hopefully I have guessed right:)
Otherwise, it's a lot of modules to include...

The project works pretty cool!

Mark

EDIT: I re-read the first post! Sorry, I forgot you had the Library already compiled!!!

I'll hopefully (No guarantees)in the future, will pay better attention.
 
Last edited:

DieterR

Member
Licensed User
Has anyone an idea what the reason for this runtime error is? "java.lang.ClassNotFoundException: javafx$print$Printer"
It occures always, regardless if I use the lib in my program or when I try any of the code samples of this thread.

Many thanks for any help.
DieterR
 
Top