PDF printing A Complete Guide
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.
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) {
//...
}
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 given 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.
PrintRequestAttributeSet attributeSet = new HashPrintRequestAttributeSet();
JobName jobName = new JobName("Example Print", null);
attributeSet.add(jobName);
The PdfDecoder class has many methods that you can call to adjust the print output such as whether you want to print to papers of different sizes or how you want the pages rotated. The most commonly used of these methods are listed below.
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());
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.
PrintService[] services = PrintServiceLookup.lookupPrintServices(DocFlavor.SERVICE_FORMATTED.PAGEABLE, attributeSet);
for(PrintService s : services) {
System.out.println(s.getName());
}
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.
PrintService printingDevice;
for(PrintService s : services) {
if(s.getName().equals("Microsoft XPS Document Writer")) {
printingDevice = s;
}
}
Step 4. Create a Pageable PDF
To create an 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 into a SimpleDoc 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 the PrintService print method with using your SimpleDoc and your attributes, if you have any, as the methods input variables.
DocPrintJob printJob = printingDevice.createPrintJob();
try {
printJob.print(doc, attributeSet);
} catch (PrintException e) {
//...
}
PDF Printing tutorials
The tutorials show you how to easily add Print PDF capabilities to your Java software and customise it with JPedal.
- PDF Print Examples lists the various printing examples available in JPedal.
- PDF Print FAQ lists common questions about printing and tips.