A printing task usually consists of two parts.

  • Job Control
  • Page Imaging

Let's start with Page Imaging.

First, we must use the interface Printable to create a printable object. We will need to import the Printable interface.

The code below shows how to import the Printable interface, implement Printable interface, and override the abstract method print.


import java.awt.print.Printable;
import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
import java.awt.Graphics;
import java.awt.Graphics2D; public class Example implements Printable { public Example() { } public int print(Graphics g, PageFormat pf, int page) throws PrinterException { return 0; } }

Notice that it uses java.awt.Graphics, java.awt.print.PageFormat, and an int value for the amount of pages. It will also throw an exception of java.awt.print.PrinterException.

Next, we will add code inside print method that will be what we actually print when the print job is started.

By using a Graphic instance within the print method, we are able to print essentially the same way as when we write graphics to the screen.


    public int print(Graphics g, PageFormat pf, int page) throws PrinterException
    {
        // This code means to return NO_SUCH_PAGE when 
        // we have exceeded the number of pages.
        // Right now, we only have 1 page.
        if (page > 0)
        {
             return NO_SUCH_PAGE;
        }
        
        // PageFormat is used to format the page. We need to
        // get the x and y values that will allow us to draw
        // within the page format's boundaries.
        Graphics2D g2d = (Graphics2D)g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());
        
        // Here is where we do whatever we want for drawing.
        // Just like when we draw to the screen.
        g.drawString("This is an example", 50, 50);
        
        // Tell the caller of this method its a page within
        // the document.
        return PAGE_EXISTS;
    }

Next, we need to create a print job. This is the second part of the two part process. Here is what it would look like.


    public void printing()
    {      
         PrinterJob job = PrinterJob.getPrinterJob();
         job.setPrintable(this);
         boolean ok = job.printDialog();
         if (ok)
         {
             try
             {
                  job.print();
             } 
             catch (PrinterException ex)
             {
                 /// Error printing
                 ex.printStackTrace();
             }
         }
    }

I just created a method called printing and added a PrinterJob. You will need to add the import for the PrinterJob.

That's really it. Not very difficult. Simple as pie.

Here is a working example of this tutorial based on the code above.

Example.class


import java.awt.print.Printable;
import java.awt.print.PageFormat;
import java.awt.print.PrinterJob;
import java.awt.print.PrinterException;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;


public class Example implements Printable, ActionListener
{ 
    public Example()
    {

    }
    
    public static void main ( String[] args )
    {
        Example example = new Example();
        
        JFrame frame = new JFrame();
        JButton button = new JButton("Print");
        button.addActionListener(example);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(button);
        frame.setSize(300,300);
        frame.setVisible(true);      
    }   
    
    public int print(Graphics g, PageFormat pf, int page) throws PrinterException
    {
        // This code means to return NO_SUCH_PAGE when 
        // we have exceeded the number of pages.
        // Right now, we only have 1 page.
        if (page > 0)
        {
             return NO_SUCH_PAGE;
        }
        
        // PageFormat is used to format the page. We need to
        // get the x and y values that will allow us to draw
        // within the page format's boundaries.
        Graphics2D g2d = (Graphics2D)g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());
        
        // Here is where we do whatever we want for drawing.
        // Just like when we draw to the screen.
        g.drawString("This is an example", 50, 50);
        
        // Tell the caller of this method its a page within
        // the document.
        return PAGE_EXISTS;
    }
    
    public void printing()
    {      
         PrinterJob job = PrinterJob.getPrinterJob();
         job.setPrintable(this);
         boolean ok = job.printDialog();
         if (ok)
         {
             try
             {
                  job.print();
             } 
             catch (PrinterException ex)
             {
                 /// Error printing
                 ex.printStackTrace();
             }
         }
    }
    
    public void actionPerformed(ActionEvent e) 
    {
        printing();
    }
}

Have a great day!