PDFOne (for Java)
Create, edit, view, print & enhance PDF documents and forms in Java SE/EE
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;

public class PDFOne_PdfPrinter_Demo {
   static {

   public static void main(String[] args) {
     PdfDocument d = new PdfDocument();    
     try {
        // Load a PDF document
        // Create a PDF printer object
        PdfPrinter printer = new PdfPrinter();

        // Specify the document that needs to be printed

        // Select a printer
           // Name of first printer 
        // Set margins
           // Left, top, right, bottom margins
           new double[] {1, 0.5, 1, 0.5}, 
           // Measurement units

        // Specify page size
        // Specify page orientation
        // Specify pages that need to be printed
        // Specify number of copies
        // Specify scaling
        // Specify how page of different copies need to be collated
        // Specify paper bin
        // Specify printing order
        // Specify which sides of paper need to be printed on
        // Show printer dialog to user
     catch (Exception ex1) {        
     } finally {
           try {
              // Release I/O resources
           } catch (Exception ex2) {

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.


Our Cloud-based document-processing
Gnostice StarDocs

Gnostice StarDocs is a scalable, fault-tolerant document-processing server solution, designed to be the central go-to place for all kinds of electronic document-processing work. StarDocs includes a web application (as seen at stardocs.gnostice.com) that frees your end-users from the need to have format-specific software to merge, split, redact, convert, edit, and sign documents - all they need is a network connection and a HTML5 browser. StarDocs provides a REST-based API that your developers can use to add document-processing functions to a variety of software applications. As the REST API is platform agnostic, your developers can write software on a wide variety of platforms - from desktop to mobile devices to Web services in any development language. To get them started quickly, StarDocs is supported by language-specific native SDK. StarDocs document server has load-balancing built-in and is designed to be scalable across enterprise Intranets and the Internet. StarDocs is available as packaged software for on-premise hosting and also as a subscription-based cloud service.

Our Delphi/C++Builder developer tools
XtremeDocumentStudio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

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.

Our .NET Developer Tools
XtremeDocumentStudio .NET

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


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

Our Java developer tools
XtremeDocumentStudio (for Java)

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

PDFOne (for Java)

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

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