Class Printer

java.lang.Object
com.codename1.printing.Printer

public final class Printer extends Object

Cross platform printing API that hands a document to the platform printing system, typically by popping up the native print dialog where the user picks a printer and options.

The document is a file in FileSystemStorage identified by its path and mime type. All platforms accept PDF (application/pdf) and common image types (image/png, image/jpeg); other mime types fail with PrintResult.STATUS_FAILED on platforms that can't render them.

Sample usage:

if (Printer.isPrintingSupported()) {
    Printer.print(reportPath, "application/pdf", new PrintResultListener() {
        public void onResult(PrintResult result) {
            if (result.isFailed()) {
                ToastBar.showErrorMessage("Print failed: " + result.getError());
            }
        }
    });
}
  • Method Details

    • isPrintingSupported

      public static boolean isPrintingSupported()
      Returns true if the underlying platform can print documents. When this returns false print(String, String, PrintResultListener) reports PrintResult.STATUS_FAILED to its listener without showing any UI.
    • print

      public static void print(String filePath, String mimeType, PrintResultListener listener)

      Print a document file through the platform printing system, typically showing the native print dialog.

      Parameters
      • filePath: path of the document in FileSystemStorage

      • mimeType: the document type, e.g. application/pdf, image/png

      • listener: callback for the print outcome, invoked on the EDT. May be null.

    • printPDF

      public static void printPDF(String filePath, PrintResultListener listener)
      Convenience variant of print(String,String,PrintResultListener) for PDF documents.
    • printImage

      public static void printImage(Image image, PrintResultListener listener)

      Print an image through the platform printing system. The image is encoded to a temporary PNG file that is deleted once the print flow finishes.

      Parameters
      • image: the image to print

      • listener: callback for the print outcome, invoked on the EDT. May be null.