PDFOne (for Java™)
Create, edit, view, print & enhance PDF documents and forms on Java™ platform
Compatibility
J2SE J2EE Windows Linux Mac (OS X)

How to Print a PDF Document in Java

Learn about PDF printing in Java.
By Lokesh Vardhan Yadav

The PDF printer component in Gnostice PDFOne (for the Java™ platform) was introduced in February 2008 with Version 2.0. Since then, the printer component has seen several improvements. In this article, we will see a code snippet that shows you how to leverage many of those enhancements.

To print a PDF document:

  1. Create a printer object
  2. Set a PdfDocument object of a loaded PDF document to the printer object.
  3. Select a physical printer for use with the printer object.
  4. Set printer properties. (Perform this step after selecting the physical printer. Only then will you be able to override loaded defaults of the physical printer.)
  5. Display the built-in "Print" dialog.

import javax.print.attribute.standard.MediaTray;
import javax.print.attribute.standard.SheetCollate;
import javax.print.attribute.standard.Sides;

import com.gnostice.pdfone.PDFOne;
import com.gnostice.pdfone.PdfDocument;
import com.gnostice.pdfone.PdfPageSize;
import com.gnostice.pdfone.PdfPrinter;
import com.gnostice.pdfone.PdfReader;

public class PDFOne_PdfPrinter_Demo {
   static {
      PDFOne.activate("your-pdfone-activation-key",
                      "your-pdfone-product-key");
   }

   public static void main(String[] args) {
    
     // Create a PDF reader object
     PdfReader reader = null;
    
     try {
         
        // Specify file that needs to be printed  
        reader = PdfReader.fileReader("sample_doc.pdf");
      
        // Create a PDF document object with the reader
        PdfDocument d = new PdfDocument(reader);
      
        // Create a PDF printer object
        PdfPrinter printer = new PdfPrinter();

        // Specify the document that needs to be printed
        printer.setDocument(d);        

        // Select a printer
        printer.setSelectedPrinterName(
           // Name of first printer 
           printer.getAvailablePrinterNames()[0]);
                  
        // Set margins
        printer.setPageMargins(
           // Left, top, right, bottom margins
           new double[] {1, 0.5, 1, 0.5}, 
           // Measurement units
           PdfPrinter.MU_INCHES);                

        // Specify page size
        printer.setPageSize(PdfPageSize.A4);
        // Specify page orientation
        printer.setOrientation(PdfPrinter.Orientation_LANDSCAPE);
        // Specify pages that need to be printed
        printer.setPageRange("1-8");
        // Specify number of copies
        printer.setCopies(3);
        // Specify scaling
        printer.setPageScale(PdfPrinter.SCALE_REDUCE_TO_PRINTER_MARGINS);
        // Specify how page of different copies need to be collated
        printer.setPrintSheetCollate(SheetCollate.COLLATED);
        // Specify paper bin
        printer.setPrintMediaTray(MediaTray.SIDE);
        // Specify printing order
        printer.setReverse(true);
        // Specify which sides of paper need to be printed on
        printer.setPrintSides(Sides.TWO_SIDED_SHORT_EDGE);
        
        // Show printer dialog to user
        printer.showPrintDialog();        
     }
     catch (Exception ex1) {        
        System.out.println(ex1.getMessage());        
     } finally {
        if (reader != null) {
           try {
              // Release I/O resources
              reader.dispose();   
           } catch (Exception ex2) {
              System.out.println(ex2.getMessage());
           }
        }
     }
  }
}

With the "Print" dialog, the actual print command is given by the user. For non-interactive or fully programmatic printing, you can use the PdfPrinter.print(String pageRange, int numOfCopies) method.

---o0O0o---

Our Free Online Document-Processing Tools
Gnostice Apps

Gnostice Apps provides several PDF document-processing services over the Web. It enables you to merge, split, convert, secure and redact PDF documents - all for FREE. All you need to access these services is a HTML5-capable browser such as the latest version of Firefox/Seamonkey or IE 11.

Our Developer Tools
eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

XtremePDFConverter VCL

A Delphi/C++Builder component to intelligently convert PDF to user-friendly Word RTF documents.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

XtremeDocumentStudio .NET

Multi-format document-processing component suite for .NET developers

PDFOne (for Java™)

A Java™ PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java™ applications.

XtremeFontEngine (for Java)

Java font engine to render glyphs from Type 1, Type 2 (CFF), and TrueType fonts

Our Office Productivity Applications
Free PDF Reader

A free, fast, and portable application for viewing, printing and converting PDF documents.

Privacy | Legal | Feedback | Newsletter | Resellers © 2002-2014 Gnostice Information Technologies Private Limited. All rights reserved.

This site is best viewed on a screen with minimum resolution of 1152 x 864 pixels. Windows XP users are advised to use Microsoft ClearType Tuning for optimal experience. Also, please use the latest version of a standards-compliant browser such as Firefox, Opera, or Dragon (Chromium).