Printing PDF tutorial
Printing PDF files is built into JPedal. Java offers a printing solution called Java Printing Services. JPedal supports this using both the Pageable (not 1.3) and Printable interfaces (but it does not currently support the Book object). We recommend the Pageable mode wherever possible as it is more robust.
What can it do?
Print PDF files as high quality output both with or without user interaction. It offers all the PDF print modes available in Acrobat (there are over 50 combinations).
- Client printing with user-selectable options
- Server printing with no user intervention
- Optimisations for PCL printers
- Define multiple page sizes
- Multiple scaling options (no scaling, rotate and fit to page, scale to paper size)
- Print ranges, only odd, only even pages
- Standard Java printing dialog or custom 'Acrobat-style' print dialog
- Modes to use Java fonts for standard fonts to speed-up printing and improve quality on low resolution devices.
- Replace form components in printing with your own customised version
PDF printing FAQ
Here is a list of general questions along with links to example source code. Please contact us if you have an additional question.
How do I find out what printers are accessible?
The PrinterJob class provides access to the printing system, allowing the user to list and select a printer using the code snippet here
The printout does not fit correctly onto the page
Java Printing allows the user to set a page size and a visible page size which JPedal will use for printing. This can be read using
PrinterJob printJob = PrinterJob.getPrinterJob();
PageFormat pf = printJob.defaultPage();
The user can also define their own page sizes to give a best fit (this is what the SilentPrint .java example code does).
//Create default Page format A4
Paper paper = new Paper();
//A4 borderless (setting may need adjustment)
paper.setSize(595, 842);
//will make part of page disappear
//paper.setImageableArea(0, 0, 595, 842);
//works on our printers
paper.setImageableArea(5, 5, 565, 812);
These values will be ignored if inappropriate for a specific printer, resulting in the image being clipped. So either, use the defaults of test for range of printers.
How can I see the actual area printed onto?
The PdfDecoder class has a method showImageableArea() which will draw a Rectangle with a diagonal cross to show the Page area. This is useful for debugging.
How do I select a printer?
This code will select the printer if it is available.
Starting with version 3.82b37 the printer can be set using JVM flags, list can be found here.
Printing using pageable (recommend print method)
To use pageable, you need to pass the instance of the Class which implements pageable into the PrinterJob object as in this example.
Printing using printable (included for backward compatability)
To use printable, you need to pass the instance of the Class which implements printable into the PrinterJob object as in this example.
Print Dialog Options
The standard Java print dialog appears in the Standard version and offers little extra functionality for allowingthe user to set scaling, rotation, etc. It can be accessed with this example.
The commercial versions of JPedal include a custom Print Dialog with support for the options as Acrobat. It has lots of bells and whistles to provide scaling, rotation, and lots of other features for the user to select.
Setting scaling
To set scaling, JPedal has a method to pass in a constant from org.jpedal.objects.PrinterOptions. This must be set before printing. This can be read from the printer dialog. There is also an option to center on scaling.
Auto rotate and centre option
Auto rotate and center option is off as a default but can be set as in this example.
Print view
JPedal can print only the part of the page currently displayed as in example.
Use PDF size for Page
Paper size is used as the default but the PDF size can be set as in this example.
Setting page size
Page size in normally part of the Paper object and can be read from the PrinterJob object and altered. Values will not over-ride any hard-coded limits on the printer. The org.jpedal.examples.simpleviewer.paper.PaperSizes class contains multiple settings for different page layouts. Always use setImageableArea() as well as the size. It can be set for printing as in this example.
Starting with version 3.82b37 the paper size can be set using JVM flags, more information can be found here.
Orientation
JPedal automatically calculates the LANDSCAPE/PORTRAIT setting to use page on the page size. We recommend you do not set this manually.
Printing a range of pages
JPedal can print only the part of the page currently displayed as well as just odd or even page. This is shown in example code.
Reducing printing size and speed
To improve this we have added some modes which speedup the process using Java's font renderer wherever possible. This is enabled using these methods org/jpedal/PdfDecoder.html#setPrintPageMode(int) with values PdfDecoder.NOTEXTPRINT (normal mode - only needed to reset), PdfDecoder.TEXTGLYPHPRINT (intermediate mode - let Java create Glyphs if font matches), PdfDecoder.TEXTSTRINGPRINT (try and get Java to do all the work). We have found PCL printing especially can be very slow any benefits from these modes, but it will work on all printers.
Release 3.79b16 adds PdfDecoder.STANDARDTEXTSTRINGPRINT which will try to print all 14 standard fonts (ie Arial, Courier, Times New Roman) using standard Java fonts even if embedded. This may not work if the font has custom encoding.
Over-Printing (also works for screen display)
Draw additional objects explains how to draw your own objects ontop of the PDF display (ie special Annotations, copyright notice).
Writing your own custom print code
If you are writing your own custom code and try to print some pages before the PDF, the whole PDF will not print out. You may need to tell JPedal you have already printed some pages using the PdfDecoder method useLogicalPrintOffset(int pagesPrinted)
Printing forms using custom code
The CustomFormPrint interface allows you to totally control the printing of the form widgets, over-riding any or all. If you pass an instance of a custom inteface into JPedal, it will be called when printing occurs.
Click here for full details and tutorial.
Related links
SimpleViewer examplealso includes code for printing and contains a custom printDialog with access to all the print functionality.
SilentPrint example provides example code for serverside printing.
Print tutorial written by Sun as general introduction











