B4J Library Simple Native Print Document Library

Hello everyone,
Here is a really simple library for printing your layouts or images with ease in B4J. This library is a slightly expanded version of the print routine Inline Java code that can be found in the B4X 3D Charts library. This library came about simply because I learned that printing a document takes just a few lines of Inline Java code.

NativePrinter library uses the native JavaFX Printing API (javafx.print) to interact with the operating system's print spooler. Specifically, it relies on the javafx.print.PrinterJob class. When you call the print method, the library creates a PrinterJob, it's as simple as that.

This library is basically a simplified version of @stevel05 excellent B4J Print JavaFX8 library which I've been using for years. You should also look at that library too.

With this library, you can set and print to any of the following preset paper sizes. A0, A1, A2, A3, A4, A5, A6, LEGAL, LETTER, NA_LETTER, TABLOID, and JAPANESE_POSTCARD. Alternatively, you can create your own custom paper sizes for labels, ID cards etc. I've attached a PDF file generated using the provided code example.

B4J library tab
1792803005699.png


Declare the object reference
B4X:
    Private NP As NativePrinter

Initialise the object
B4X:
    NP.Initialize

Print the currently displayed page
B4X:
Private Sub Button1_Click
    NP.PrintContent(Root, True, False, "A4", 20, 20, 20, 20, "HIGH", "Print Form...")
End Sub

Print an ImageView
B4X:
Private Sub Button2_Click
    Dim IV As ImageView
        IV.Initialize("")
        IV.SetSize(555, 810) 'A great ImageView size for printing on A4 paper
        IV.SetImage(XUI.LoadBitmap(File.DirAssets, "Invoice.jpg"))
        IV.PreserveRatio = True

    NP.PrintContent(IV, True, False, "A4", 20, 20, 20, 20, "HIGH", "Print Imageview...")
End Sub

B4J example test app screenshot - Printing a loaded layout file
1792804750379.png


SS_SimpleNativePrinter

Author:
Peter Simpson
Version: 1.0
  • NativePrinter
    • Functions:
      • Class_Globals As String
      • GetDefaultPrinter As String
        Returns the name of the system default printer.
      • GetPrinters As List
        Returns a list of all installed printers on the system.
      • Initialize As String
        Initializes the object. You can add parameters to this method if needed.
      • IsInitialized As Boolean
        Tests whether the object has been initialized.
      • PrintContent (Target As Object, ShowDialog As Boolean, IsLandscape As Boolean, PaperSize As String, Top As Double, Right As Double, Bottom As Double, Left As Double, Quality As String, JobName As String) As String
        Prints a UI layout, form, or image using native JavaFX printing.
        Target: The object to print. Pass a layout root (B4XView) or an ImageView containing a bitmap.
        ShowDialog: True to show the system print dialog, False to print directly to the default printer.
        IsLandscape: True for landscape orientation, False for portrait.
        PaperSize: Standard paper size as a string (e.g., "A4", "A3", "LETTER", "LEGAL").
        Top, Right, Bottom, Left: Margins in typographical points (72 points = 1 inch).
        Quality: Print quality level. Options: "HIGH", "NORMAL", "LOW", or "DRAFT".
        JobName: The name of the print job (used for the print queue or default PDF filename).
      • PrintCustomContent (Target As Object, ShowDialog As Boolean, IsLandscape As Boolean, PaperWidth As Double, PaperHeight As Double, Top As Double, Right As Double, Bottom As Double, Left As Double, Quality As String, JobName As String) As String
        Prints a UI layout, form, or image using a custom paper size (finds the closest match supported by the printer).
        Target: The object to print (B4XView or ImageView).
        ShowDialog: True to show the system print dialog, False to print directly to the default printer.
        IsLandscape: True for landscape orientation, False for portrait.
        PaperWidth, PaperHeight: Custom dimensions in points (1 mm = 2.83465 points).
        Top, Right, Bottom, Left: Margins in points.
        Quality: Print quality level ("HIGH", "NORMAL", "LOW", or "DRAFT").
        JobName: The name of the print job.
      • SetPrinter (PrinterName As String) As String
        Sets the printer to use by name before printing.
        Pass an empty string ("") to revert to the system default printer.
      • Snapshot (TargetView As B4XView) As B4XBitmap
        Takes a full high quality snapshot of B4XViews and returns a B4XBitmap.
        This method takes a higher quality snapshot than for example Root.Snapshot.
        Example: Snapshot(Root), or basically any other view.
        Uses slightly more memory for higher quality screenshots.

Enjoy...
 

Attachments

Last edited:
what is the SS_EasyModBusClient - lib for and how to use it ?
 
Last edited:
Hello GMan,
what is the SS_EasyModBusClient - lib for and how to use it ?

I've looked through this library and cannot find a reference to SS_EasyModBusClient. Where did you find this reference with this library?

Modbus is a communication protocol that allows developers to create solutions to connect to and control hardware, especially industrial equipment in factories and manufacturing plants etc.
 
Hello GMan,


I've looked through this library and cannot find a reference to SS_EasyModBusClient. Where did you find this reference with this library?

Modbus is a communication protocol that allows developers to create solutions to connect to and control hardware, especially industrial equipment in factories and manufacturing plants etc.
It was in the ZIP File SS_SimpleNativePrinter_Lib
 
Back
Top