fitting PrinterJob Object to specific print format of BufferedImage

淺唱寂寞╮ 提交于 2019-11-29 18:39:57
MadProgrammer

First of printing, "Printing is Fun" - remember, repeat this at 2am...

Basically, you need to start by converting your paper size from CM to pixels. Java uses 72dpi for it's API.

So you page size of 8.6x5.4cm calculates to 153.0708659856 x 243.7795273104 pixels

Then you need to create a new Paper that meets those page requirements...

This all needs to be wrapped back into a PageFormat and handed back to the PrintJob.

Within the Printable, you need to scale the image to "fit" the printable area. Image scaling is fun...

So, for me test, I had an image of 800x1159, which was scaled down to 166x241

For a better discussion on image scaling check out this question

public class PrintTest02 {

    private static BufferedImage image;

    public static void main(String[] args) {
        try {
            image = ImageIO.read(new File("/path/to/image.png"));

            System.out.println(image.getWidth() + "x" + image.getHeight());

            PrinterJob pj = PrinterJob.getPrinterJob();
            if (pj.printDialog()) {
                PageFormat pf = pj.defaultPage();
                Paper paper = pf.getPaper();
//                        86X54mm
                double width = fromCMToPPI(8.6);
                double height = fromCMToPPI(5.4);
                paper.setSize(width, height);
                paper.setImageableArea(
                                fromCMToPPI(0.1),
                                fromCMToPPI(0.1),
                                width - fromCMToPPI(0.1),
                                height - fromCMToPPI(0.1));
                pf.setOrientation(PageFormat.PORTRAIT);
                pf.setPaper(paper);
                PageFormat validatePage = pj.validatePage(pf);
                System.out.println("Valid- " + dump(validatePage));
                pj.setPrintable(new MyPrintable(), validatePage);
                try {
                    pj.print();
                } catch (PrinterException ex) {
                    ex.printStackTrace();
                }
            }
        } catch (IOException exp) {
            exp.printStackTrace();
        }
    }

    protected static double fromPPItoCM(double dpi) {
        return dpi / 72 / 0.393700787;
    }

    protected static double fromCMToPPI(double cm) {
        return toPPI(cm * 0.393700787);
    }

    protected static double toPPI(double inch) {
        return inch * 72d;
    }

    protected static String dump(Paper paper) {
        StringBuilder sb = new StringBuilder(64);
        sb.append(paper.getWidth()).append("x").append(paper.getHeight())
                        .append("/").append(paper.getImageableX()).append("x").
                        append(paper.getImageableY()).append(" - ").append(paper
                        .getImageableWidth()).append("x").append(paper.getImageableHeight());
        return sb.toString();
    }

    protected static String dump(PageFormat pf) {
        Paper paper = pf.getPaper();
        return dump(paper);
    }

    public static class MyPrintable implements Printable {

        @Override
        public int print(Graphics graphics, PageFormat pageFormat,
                                         int pageIndex) throws PrinterException {
            System.out.println(pageIndex);
            int result = NO_SUCH_PAGE;
            if (pageIndex < 1) {
                Graphics2D g2d = (Graphics2D) graphics;
                System.out.println("[Print] " + dump(pageFormat));
                double width = pageFormat.getImageableWidth();
                double height = pageFormat.getImageableHeight();

                System.out.println("Print Size = " + fromPPItoCM(width) + "x" + fromPPItoCM(height));
                g2d.translate((int) pageFormat.getImageableX(),
                                (int) pageFormat.getImageableY());
                Image scaled = null;
                if (width > height) {
                    scaled = image.getScaledInstance((int)Math.round(width), -1, Image.SCALE_SMOOTH);
                } else {
                    scaled = image.getScaledInstance(-1, (int)Math.round(height), Image.SCALE_SMOOTH);
                }
                g2d.drawImage(scaled, 0, 0, null);
                result = PAGE_EXISTS;
            }
            return result;
        }

    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!