The Ultimate Java PDF Library SDK

Seamless PDF Integration for Java

Download Trial Purchase

PDF Printing – Quick Start Tutorial

This tutorial aims to show you how to use Java and JPedal to print a PDF file in 5 simple steps. These 5 steps will give you greater control over how the printing takes place. If you do not require such in depth control you can print a PDF file in just a couple of lines of code using the print example.

 

Step 1.  Create a PdfDecoder object

First you need a PdfDecoder object to represent your PDF file. You may also need to set up font replacements if required.

 
Important note on printing Encrypted PDF files
If the password requires a password then you will need to set the password with setEncryptionPassword(String password) or open the file using openPdfFile(String filename, String password) instead of the method shown below.

 

Step 2.  Set your attributes

You will also need an AttributeSet which will tell the printer what settings you wish to use such as printing to a give size of paper or the number of copies to print. For this tutorial we will only be using the JobName attribute.
Click here for more information on the various attributes available.

The PdfDecoder class has many methods that you can call to adjust the print output such as whether you want to print to paper of different sizes or how you want the pages rotated. The most commonly used of these methods are listed below.

 

Step 3.  Find a printer

To find the list of PrintServices available to Java that supports the given attributes you have set you will need the following section of code, here we are just outputting the printer names to the console.

Once you know which printers you have access to you can use these names to acquire a particular PrintService with a specified name as shown below.

 

Step 4.  Create a Pageable PDF

To create a object that can be printed you need to create a PdfBook object (The attributes parameter may be null if desired). This is then be passed in to a SimpleDoc for printing.

 

Step 5.  Print it!

All you need to do here is receive a DocPrintJob from the PrintService and then call the PrintService print method with using your SimpleDoc and your attributes, if you have any, as the methods input variables.

 

PDF Printing tutorials

The tutorials show you how to easily add Print PDF capabilities to your Java software and customise it with JPedal.
Print PDF
PDF Print Examples lists the various printing examples available in JPedal.

PDF Print FAQ lists common questions about printing and tips.

IDRSolutions Limited 1999-2016