B4J Library B4j Print JavaFX8

Discussion in 'B4J Libraries & Classes' started by stevel05, Jan 25, 2015.

  1. stevel05

    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:
    Code:
    Dim P As Printer = Printer_Static.GetDefaultPrinter
        
    Dim PJ As PrinterJob = PrinterJob_Static.CreatePrinterJob2(P)
        PJ.PrintPage(lblTest)
        PJ.EndJob
    Or with dialogs:

    Code:
    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
     

    Attached Files:

    Last edited: Mar 7, 2015
    PatrikCavina, sz4t4n, Ferbis and 11 others like this.
  2. stevel05

    stevel05 Expert Licensed User

    Updated to fix a small bug. Probably more of these to come.:)
     
  3. swissmade

    swissmade Active Member Licensed User

    Very nice and useful thanks.;);)
     
  4. swissmade

    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.
     
  5. stevel05

    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.
     
  6. swissmade

    swissmade Active Member Licensed User

    Using this I get a error that the XML was improperly formatted. Please escape it if necessary
    I using the test program you made. Without Library.
     
  7. swissmade

    swissmade Active Member Licensed User

    A small sample can be nice.
    Many thanks for helping me out here.
     
  8. stevel05

    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.
     
  9. swissmade

    swissmade Active Member Licensed User

    ;);)
     
  10. swissmade

    swissmade Active Member Licensed User

    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.
    Code:
    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
     
  11. stevel05

    stevel05 Expert Licensed User

    How are you then printing the page?
     
    swissmade likes this.
  12. swissmade

    swissmade Active Member Licensed User

    I think I have seen it:)
    Code:
    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.
     
  13. swissmade

    swissmade Active Member Licensed User

    It is working.
    Never too old to learn.
    Many thanks to show me the way.;);)
     
    Theera and stevel05 like this.
  14. swissmade

    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.
     
  15. stevel05

    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:

    Code:
    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.
     
  16. swissmade

    swissmade Active Member Licensed User

    Nice one

    Many thanks.;)
     
  17. swissmade

    swissmade Active Member Licensed User

  18. swissmade

    swissmade Active Member Licensed User

    Solution to Set your one Papersize
    Code:
    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.
    Code:
    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: Mar 7, 2015
    Theera and stevel05 like this.
  19. Mark Zraik

    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: Mar 22, 2015
  20. DieterR

    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
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice