Please choose a menu option
Visit the IDRsolutions WebSite

PDF Printing - Tutorial

This tutorial aims to show you how to use Java and JPedal to print a PDF file.

 

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 with setEncryptionPassword(String password) or openFile with a password.

 

Step 2.  Find a print service

The simplest way to acquire a PrintService is to get the default.

PrintService printingDevice = PrintServiceLookup.lookupDefaultPrintService();

Alternatively, if you have multiple printers available you could select like so:

PrintService[] services = PrintServiceLookup.lookupPrintServices(DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
for(PrintService s : services) {
    if(s.getName().equals("Microsoft XPS Document Writer") {
        printingDevice = s;
    }
}

 

Step 3.  Set your attributes

You may also want an PrintRequestAttributeSet which will tell the printer what kind of things you like it to do. For example, print in monochrome or set a number of copies. For further information on attributes you can see the extended version in the SilentPrint example. 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);

 

Step 4.  Set PDF Print settings

JPedal has a number of settings to mimic Acrobats printing methods:

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);

 

Step 5.  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 6.  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) {
...
}

 

Please feel free to look at the SilentPrint example of using the settings.

Feedback, questions and updates

There is a support forum for questions, or you can contact IDRsolutions via our contact page.

There is an RSS feed with regular updates about our products. We recommend you subscribe so we can keep you updated.

Please let us know if you find any bugs with the bug report form.

 

Return to main PDF support section

Click here for the main PDF support area with lots of java examples, PDF tutorials and useful information to get the most out of the JPedal PDF library

 


PDF viewer