Link
Skip to main content

PDF Printing FAQs

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 PDF printout does not fit correctly onto the page

Java Printing allows the user to set 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 the best fit:

//Create default Page format A4
Paper paper = new Paper();

//A4 borderless (setting may need adjustment)
paper.setSize(595, 842);

//the following 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 or test for a range of printers.

How do I select a PDF printer?

This code will select the printer if it is available.

The printer can be set using JVM flags, the list of JVM flags can be found here.

PDF Print Dialog Options

The JPedal Viewer has a feature-packed print panel allowing you detailed control over the printing of your PDF documents and displays a preview of the output with your currently selected settings.

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.

How do I print in monochrome?

This code shows how to print to monochrome.

Auto-rotate and centre option

Auto-rotate and centre option is off as a default but can be set as in this example.

Auto-rotate or centre options can also be set independently of each other using the following methods.

//Set auto rotate
PdfDecoder.setPrintAutoRotate(boolean rotate);

//Set print centering
PdfDecoder.setCenterOnScaling(boolean center);

PDF Print view

JPedal can print only the part of the page currently displayed as in this example.

//Print only the area of the page currently visible in the viewer, instead of the whole page
decodePdf.setPrintCurrentView(true);

Use PDF size for Printer paper size

Paper size is used as the default but the PDF size can be set as in this example.

//Use the PDF page size instead of the printer's default paper size
decodePdf.setUsePDFPaperSize(true);

Setting a left margin for duplex printing

setPrintIndent(int oddPages, int evenPages) sets a left margin, in points, for printing odd and even pages separately. This is useful when duplex (double-sided) printing needs extra room for binding.

decodePdf.setPrintIndent(20, 10);

Printing pages of different sizes

By default, JPedal prints every page in a document at the same size. If your document contains pages of different sizes and you want each to print at its own size, use setAllowDifferentPrintPageSizes(boolean).

decodePdf.setAllowDifferentPrintPageSizes(true);

Tracking printing progress

getCurrentPrintPage() returns the page currently being printed, or -1 once printing has finished. This can be polled from another thread to show progress while a large document prints.

final int pageBeingPrinted = decodePdf.getCurrentPrintPage();

If you reuse the same PdfDecoder to print more than once, call resetCurrentPrintPage() beforehand so that getCurrentPrintPage() starts tracking from the first page again.

decodePdf.resetCurrentPrintPage();

Setting PDF page size

PDF 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.viewer.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.

The paper size can be set using JVM flags, more information can be found here.

PDF Page Orientation

JPedal automatically calculates the LANDSCAPE/PORTRAIT setting to use based on the PDF page size. We recommend you do not set this manually.

Printing a range of pages

JPedal can print only the part of the PDF page currently displayed as well as just odd or even page. This is shown in example code.

Reducing PDF printing size and speed

To improve this we have added some modes which speed up the process using Java’s font renderer wherever possible. This is enabled using the method org/jpedal/PdfDecoder.html#setPrintPageMode(int) with one of the following values.

We have found PCL printing especially can be very slow and benefits from these modes, but it will work on all printers.

Over-Printing (also works for screen display)

Draw additional objects explains how to draw your own objects on top of the PDF printout (ie special Annotations, copyright notice).

Writing your own custom PDF 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 PDF forms using custom code

The CustomFormPrint interface allows you to totally control the printing of the PDF form widgets, over-riding any or all. If you pass an instance of a custom interface into JPedal, it will be called when printing occurs.

PDF Printing tutorials

The tutorials show you how to easily add Print PDF capabilities to your Java software and customise it with JPedal.


Why JPedal?

  • Actively developed commercial library with full support and no third party dependencies.
  • Process PDF files up to 3x faster than alternative Java PDF libraries.
  • Simple licensing options and source code access for OEM users.

Learn more about JPedal

Start Your Free Trial


Customer Downloads

Select Download