Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To print from Java, create a PrinterJob, attach content with either setPrintable or setPageable, optionally let the user choose settings, and submit the job with print(). Use Printable when your application renders pages on demand; use Pageable when the job needs to describe page count and page-specific formats or renderers.

Java’s printing API and its main classes

Java’s java.awt.print package provides a general 2D printing API for describing printable content, setting up pages, and controlling print jobs. Oracle’s Java SE API documentation calls PrinterJob “the principal class that controls printing.” The job coordinates the printer service, content, dialogs, and submission; it does not itself draw your document.

  • PrinterJob controls a print job and submits it to a print service.
  • Printable supplies the rendering callback used to draw a page.
  • Pageable describes a set of pages, including each page’s format and painter.
  • PageFormat describes a page’s size and orientation.
  • Paper describes physical paper characteristics, including the imageable area available for printing.

These roles are documented in Oracle’s Java SE 17 java.awt.print package overview and the Java SE 24 PrinterJob reference.

How a basic print job works

PrinterJob.getPrinterJob() creates a job initially associated with the default printer, if one is available. A default printer is not guaranteed: the method can still return a job when no printer exists, while getPrintService() returns null and printing may fail. If your application must confirm that printers are available, inspect the services returned by PrinterJob.lookupPrintServices().

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A minimal job can attach a Printable and submit it without showing a dialog:

PrinterJob job = PrinterJob.getPrinterJob();

if (job.getPrintService() == null) {
    throw new IllegalStateException("No print service is available");
}

job.setPrintable(new MyPrintable());
job.print();

MyPrintable must implement the page-rendering callback described below. Real applications commonly provide a print dialog so the user can select settings before submission.

Showing a dialog and handling cancellation

printDialog() returns false when the user cancels. Do not submit the job in that case. Print dialogs can also throw HeadlessException, so code that may run without a graphical environment should avoid assuming a dialog can be shown.

PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(new MyPrintable());

if (job.getPrintService() == null) {
    throw new IllegalStateException("No print service is available");
}

if (job.printDialog()) {
    job.print();
}

Oracle documents the job creation, service discovery, dialog behavior, and print calls in its Java SE 24 PrinterJob reference. Native dialog appearance and driver behavior can vary by operating system and printer setup.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose between Printable and Pageable

The choice depends on whether the job needs only a renderer or a document-level description of its pages.

Question Printable Pageable
Who provides page count? There is no intrinsic page count in Printable; the print system requests pages from its renderer. The Pageable supplies the page count.
Can formats differ by page? A single Printable is registered with a page format, or can be registered with a PageFormat for rendering. It does not, by itself, describe a page-by-page set of formats. Yes. It provides a PageFormat for each page.
How is rendering supplied? The object implements the callback that paints requested pages. It supplies a Printable painter for each page.

Use setPrintable for a straightforward document whose application can render requested pages. Use setPageable when the print system needs a page count and page-specific rendering or layout information. The Book class is a convenient Pageable implementation for documents whose pages can use different formats or painters.

Render pages with Printable

A Printable is called when a page is to be rendered. Its print method receives a graphics context, a PageFormat, and a zero-based page index. Return PAGE_EXISTS after drawing a requested page; return NO_SUCH_PAGE when the requested index is beyond the document.

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;

final class MyPrintable implements Printable {
    @Override
    public int print(Graphics graphics, PageFormat format, int pageIndex)
            throws PrinterException {
        if (pageIndex >= 1) {
            return NO_SUCH_PAGE;
        }

        Graphics2D g2 = (Graphics2D) graphics;
        g2.drawString("Hello from Java printing", 72, 72);
        return PAGE_EXISTS;
    }
}

This example renders one page. The callback may be requested more than once as part of printing, so rendering should draw the requested page from the supplied state rather than relying on a single sequential call. The page index is not a page number to display directly unless you convert it to a human-facing numbering scheme.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Describe pages with Pageable

A Pageable gives the print system a document-level view: how many pages exist, the format of a page, and the Printable that renders it. This is useful when a document includes pages with different orientation, paper size, or rendering logic.

For simpler cases, Book can collect page painters and formats, then be attached with setPageable. The package overview describes Book as a way to represent pages with different formats or painters.

PrinterJob job = PrinterJob.getPrinterJob();
Book book = new Book();

// Add page painters and PageFormat values to the Book here.
job.setPageable(book);

if (job.printDialog()) {
    job.print();
}

The comment marks application-specific setup: the book needs pages added before it can describe a useful document. If you implement Pageable directly, provide its page count and page-specific format and painter consistently.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

PageFormat, Paper, and printable area

PageFormat describes page dimensions and orientation; its associated Paper describes the physical sheet and its imageable area. The imageable area is the region the printer can actually mark. It may be smaller than the full sheet because of printer hardware limits, even when the requested page size and margins appear valid.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Call PrinterJob.validatePage(format) when you need a format adjusted for the current printer. It returns a copy of the format that the printer can use; for example, the imageable area may be reduced to reflect the device’s limits. Do not assume this preserves every requested margin. When using Pageable, use the appropriate page formats for its pages and account for the validated printable area in your layout.

Honor settings selected in the print dialog

When using printDialog(PrintRequestAttributeSet), the dialog can modify the supplied attribute set. Pass the selected attributes to print(attributes) so they apply to the print operation. Showing a dialog alone does not automatically update every aspect of the job’s state.

import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;

PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();

if (job.printDialog(attributes)) {
    job.print(attributes);
}

If a Pageable document must change its page geometry to honor the user’s selected media, derive the relevant PageFormat from those selections and apply it to the document’s page descriptions. Passing attributes to print(attributes) is important, but it does not by itself rewrite page formats already supplied by a Pageable.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.