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 page to a given printer using the following single line of code.

SilentPrint print = new SilentPrint(String file_name, String printerName);

 

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.

PdfDecoder decodePdf = new PdfDecoder(true); //Set to true as I don’t want to render it to screen in this tutorial

try {
decodePdf.openPdfFile(“pdf/file/path.pdf”);
FontMappings.setFontReplacements();
}
catch (Exception e) {

}

 

Important note on printing Encrypted PDF files

If the PDF file is encrypted, you will need the BouncyCastle jar on your classpath and may need to set the password withsetEncryptionPassword(String password) or openFile with a password.

 

Step 2.  Set your attributes

You will also need an AttributeSet which will tell the printer what kind of things you want it to do such as print on a certain sized paper or how many copies you want. I don’t really want it to do anything for this tutorial so I’ll leave it as it is, see here for further information onattributes. In the following example I’m just going to set a JobName:

PrintRequestAttributeSet attributeSet = new HashPrintRequestAttributeSet();
JobName jobName = new JobName(“Example Print”, null);
attributeSet.add(jobName);

The PdfDecoder class has lots of methods that you can call to adjust the printout such as whether you want to print to paper of different sizes or how you want the pages rotated. Such as:

decodePdf.setPrintAutoRotateAndCenter(true);
decodePdf.setPrintPageScalingMode(PrinterOptions.PAGE_SCALING_FIT_TO_PRINTER_MARGINS);
decodePdf.setPrintPageScalingMode(PrinterOptions.PAGE_SCALING_NONE);
decodePdf.setPrintPageScalingMode(PrinterOptions.PAGE_SCALING_REDUCE_TO_PRINTER_MARGINS);
decodePdf.setPagePrintRange(1, decodePdf.getPageCount());

There are some more print settings that you can adjust described here.

 

Step 3.  Find a printer

To find the list of printers available to Java that can do what you require:

PrintService[] services = PrintServiceLookup.lookupPrintServices(DocFlavor.SERVICE_FORMATTED.PAGEABLE, attributeSet);

for(PrintService s : services) {
System.out.println(s.getName());
}

This shows the names of all the devices you can print out on. I’m going to use the Windows XPS Document Writer to print a Pdf in this example.

 

PrintService printingDevice;
for(PrintService s : services) {
    if(s.getName().equals(“Microsoft XPS Document Writer”)) {
        printingDevice = s;
    }
}

 

 

Step 4.  Create a Pageable PDF

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

PdfBook pdfBook = new PdfBook(decodePdf, printingDevice, attributeSet);
SimpleDoc doc = new SimpleDoc(pdfBook, DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);

 

Step 5.  Print it!

All you need to do here is receive a DocPrintJob from the PrintService and then call it print method with the SimpleDoc and your attributes if you have any.

DocPrintJob printJob = printingDevice.createPrintJob();

try {
printJob.print(doc, attributeSet);
} catch (PrintException e) {

}