Class PrintPdfPages

java.lang.Object
org.jpedal.examples.printing.PrintPdfPages

public class PrintPdfPages extends Object

PrintPdfPages


This class provides a simple Java API to access PDF printing features in JPEDAL via a simple API

Example 1 - Print all pages from a PDF

PrintPdfPages print=new PrintPdfPages("C:/pdfs/mypdf.pdf");
      //print.setPassword("password");
      if (print.openPDFFile()) {
          print.printAllPages("Printer Name");
      }

      print.closePDFfile(false);

Example 2 - Print a single page from a PDF


PrintPdfPages print=new PrintPdfPages("C:/pdfs/mypdf.pdf");
      //print.setPassword("password");
      if (print.openPDFFile()) {
          int pageNumber = 1;
          print.printPage("Printer Name", pageNumber);
      }

      print.closePDFfile(false);

Example 3 - Access directly from the Jar

PrintPdfPages can run from jar directly using the command and will print pages from a PDF file.

java -cp libraries_needed org/jpedal/examples/printing/PrintPdfPages inputValues

Where inputValues is 2 or 3 values:
  • First value: The PDF filename (including the path if needed). If it contains spaces it must be enclosed by double quotes (ie "C:/Path with spaces/").
  • Second value: The name of the printer to be used. If it contains spaces it must be enclosed by double quotes (ie "My Printer").
  • Third value: The page number you wish to print as an integer. This is optional, if not set all pages are printed.


See our Support Pages for more info on PDF printing.
When running on Windows, Win32PrintServiceLookup starts a daemon thread that does not close when all threads have ended. In order to prevent this thread remaining open and keeping the application open either System.exit(0) should be called or the final use of the method closePDFfile(boolean closeAllThreads) should have an input of true.
  • Constructor Summary

    Constructors
    Constructor
    Description
    PrintPdfPages(byte[] byteArray)
    Sets up an PrintPdfPages instance to open a PDF file contained as a BLOB within a byte[] stream
    Sets up an PrintPdfPages instance to open a PDF File
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    closePDFfile(boolean closeAllThreads)
    Ensure PDF file is closed once no longer needed and all resources released.
    static void
    main(String[] args)
     
    boolean
    routine to open the PDF File so we can access
    void
    printAllPages(String printerName)
    print all the pages from the open PDF file
    void
    printPage(String printerName, int page)
    print the page specified from the open PDF file
    void
    setPassword(String password)
     

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • PrintPdfPages

      public PrintPdfPages(String fileName)
      Sets up an PrintPdfPages instance to open a PDF File
      Parameters:
      fileName - full path to a single PDF file
    • PrintPdfPages

      public PrintPdfPages(byte[] byteArray)
      Sets up an PrintPdfPages instance to open a PDF file contained as a BLOB within a byte[] stream
      Parameters:
      byteArray - file as a BLOB
  • Method Details

    • main

      public static void main(String[] args)
    • setPassword

      public void setPassword(String password)
      Parameters:
      password - the USER or OWNER password for the PDF file
    • openPDFFile

      public boolean openPDFFile() throws PdfException
      routine to open the PDF File so we can access
      Returns:
      true if successful
      Throws:
      PdfException - PdfException
    • printAllPages

      public void printAllPages(String printerName) throws PrintException
      print all the pages from the open PDF file
      Parameters:
      printerName - is the printers name
      Throws:
      PrintException - PrintException
      PrintPdfPages printPages=new PrintPdfPages("C:/pdfs/mypdf.pdf");
                                          //printPages.setPassword("password");
                                          if(printPages.openPDFFile()){
                                              printPages.printAllPages("Printer Name");
                                          }
      
                                          printPages.closePDFfile(false);
    • printPage

      public void printPage(String printerName, int page) throws PrintException
      print the page specified from the open PDF file
      Parameters:
      printerName - is the printers name
      page - is the page to print
      Throws:
      PrintException - PrintException
      PrintPdfPages printPages=new PrintPdfPages("C:/pdfs/mypdf.pdf");
                                          //printPages.setPassword("password");
                                          if(printPages.openPDFFile()){
                                              printPages.printAllPages("Printer Name", 1);
                                          }
      
                                          printPages.closePDFfile(false);
    • closePDFfile

      public void closePDFfile(boolean closeAllThreads)
      Ensure PDF file is closed once no longer needed and all resources released.
      Please note, Win32PrintServiceLookup used by Java on Windows has a daemon thread that doesn't close. In order to close this thread a System.exit(0) is required to close the application otherwise the thread prevents it from closing.
      Parameters:
      closeAllThreads - Flag to call System.exit(0) when finished