0

I am printing a PNG image on the printer. The image is printed in the center of the page and does not fill the whole page. I tried increasing the size of the image but its always in the center of the page. Any ideas how to make it fit the page?

psStream = new URL(url).openStream();
                if (psStream == null) {
                    return "Unable to fetch image";
                }
                DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG;
                Doc myDoc = new SimpleDoc(psStream, flavor, null);
                PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
                PrintServiceAttributeSet attributes = new HashPrintServiceAttributeSet();
                attributes.add(new PrinterName(printData.printer, Locale.getDefault()));
                final PrintService[] printServices = PrintServiceLookup.lookupPrintServices(flavor, attributes);
                if (printServices.length == 0) {
                    return "Could not find printer " + printData.printer;
                } else {
                    myPrinter = printServices[0];
                    DocPrintJob job = myPrinter.createPrintJob();

                    try {
                        job.print(myDoc, aset);
                        return null;
                    } catch (Exception e) {
                        e.printStackTrace();
                        return "Could not print : " + e.getMessage();
                    }

                }
124697
  • 22,097
  • 68
  • 188
  • 315

2 Answers2

0

Essentially you have to resize the image to fit into the page dimensions. In addition, the printer driver defines a page border area by default. So to use the whole page you have to remove those borders. However, sometimes it's impossible to remove the borders because it's controlled by the printer's driver, so you can control just what the driver you control.

public void print(String imageFileName) throws IOException, PrinterException {
    final PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
    final BufferedImage image = ImageIO.read(new File(imageFileName));
    final PrinterJob printJob = PrinterJob.getPrinterJob();
    printJob.setJobName("MyApp: " + imageFileName);
    printJob.setPrintService(printService);
    printJob.setPrintable(new Printable() {
        @Override
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            if (pageIndex == 0) {
                final Paper paper = pageFormat.getPaper();
                paper.setImageableArea(0.0, 0.0, pageFormat.getPaper().getWidth(), pageFormat.getPaper().getHeight());
                pageFormat.setPaper(paper);
                graphics.translate((int) pageFormat.getImageableX(), (int) pageFormat.getImageableY());
                graphics.drawImage(image, 0, 0, (int) pageFormat.getPaper().getWidth(), (int) pageFormat.getPaper().getHeight(), null);
                return PAGE_EXISTS;
            } else {
                return NO_SUCH_PAGE;
            }
        }
    });
    printJob.print();
}

I guess you don't want print a stretched image, so you have to scale the image properly. There is another post that seems to be the same and show how to scale the image: Fit image into the printing area

Community
  • 1
  • 1
Wellington Souza
  • 2,200
  • 2
  • 22
  • 33
0

Here's some code that I use to print a Java Swing JPanel. You can modify it to print a BufferedImage from a png.

I keep the image proportional, so it only fills the page horizontally or vertically, but not both.

package com.ggl.sudoku.solver.controller;

import java.awt.Graphics;
import java.awt.Graphics2D;
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;

import javax.swing.JPanel;

import com.ggl.sudoku.solver.view.SudokuFrame;

public class PrintActionListener implements Runnable {

    private SudokuFrame frame;

    public PrintActionListener(SudokuFrame frame) {
        this.frame = frame;
    }

    @Override
    public void run() {
        final BufferedImage image = createPanelImage();

        PrinterJob printJob = PrinterJob.getPrinterJob();
        printJob.setPrintable(new ImagePrintable(printJob, image));

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

    private BufferedImage createPanelImage() {
        JPanel panel = frame.getSudokuPanel();
        BufferedImage image = new BufferedImage(panel.getWidth(),
                panel.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        panel.paint(g);
        g.dispose();
        return image;
    }

    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;
            }
        }

    }

}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111