1

I would like to know if there is a proper way of printing a BufferedImage in Java. Basically I have created a photo manipulation program which works fine, I can save images etc. But my real goal is to send it to the printer software so that you can select the amount of pages you want to print and page type.

So my shortened question is, how do I send a buffered image to the printer so that a printer selection screen will popup etc and then be able to print?

If anyone can show me an example of this, it would be greatly appreciated.

Maarten
  • 635
  • 1
  • 9
  • 29
  • 4
    [Printing](http://docs.oracle.com/javase/tutorial/2d/printing/) may be a good starting point. – mre Aug 23 '13 at 13:54
  • 2
    @mre this is the same tutorial I used for printing from java apps and it is very good. Let me also recommend another resource: http://www.javaworld.com/jw-10-2000/jw-1020-print.html. Both articles assume some java knowledge but no knowledge whatsoever of working with printing APIs and should be an excellent guide and foundation. – Jeremy Johnson Aug 23 '13 at 13:58

1 Answers1

12

Here's one from one of my Java projects. This code will scale and print one image on a printer page.

You call it like this:

    printButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            //Start a Thread
            new Thread(new PrintActionListener(image)).start();         
        }
    });

Here's the Runnable:

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

public class PrintActionListener implements Runnable {

    private BufferedImage       image;

    public PrintActionListener(BufferedImage image) {
        this.image = image;
    }

    @Override
    public void run() {
        PrinterJob printJob = PrinterJob.getPrinterJob();
        printJob.setPrintable(new ImagePrintable(printJob, image));

        if (printJob.printDialog()) {
            try {
                printJob.print();
            } catch (PrinterException prt) {
                prt.printStackTrace();
            }
        }
    }

    public class ImagePrintable implements Printable {

        private double          x, y, width;

        private int             orientation;

        private BufferedImage   image;

        public ImagePrintable(PrinterJob printJob, BufferedImage image) {
            PageFormat pageFormat = printJob.defaultPage();
            this.x = pageFormat.getImageableX();
            this.y = pageFormat.getImageableY();
            this.width = pageFormat.getImageableWidth();
            this.orientation = pageFormat.getOrientation();
            this.image = image;
        }

        @Override
        public int print(Graphics g, PageFormat pageFormat, int pageIndex)
                throws PrinterException {
            if (pageIndex == 0) {
                int pWidth = 0;
                int pHeight = 0;
                if (orientation == PageFormat.PORTRAIT) {
                    pWidth = (int) Math.min(width, (double) image.getWidth());
                    pHeight = pWidth * image.getHeight() / image.getWidth();
                } else {
                    pHeight = (int) Math.min(width, (double) image.getHeight());
                    pWidth = pHeight * image.getWidth() / image.getHeight();
                }
                g.drawImage(image, (int) x, (int) y, pWidth, pHeight, null);
                return PAGE_EXISTS;
            } else {
                return NO_SUCH_PAGE;
            }
        }

    }

}
Sarin Suriyakoon
  • 420
  • 1
  • 7
  • 19
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111