fitting PrinterJob Object to specific print format of BufferedImage
Asked Answered
A

1

0

Im using PrinterJob object in order to print my Bufferedimage, I have a BufferedImage which I proccess and send it to Printer job with Paper Format etc, and I cant make it fittable to my card printer. when i save it to my hard-disk and print via windows printing manager it printing very good on my card printer but with PrinterJob it came out too big and not fittable for a card

the size of the card is 86X54mm and the size of my buffered image is 1300x816px The Code :

    PrinterJob printjob = PrinterJob.getPrinterJob();
    printjob.setJobName("CardPrint");

    Printable printable = new Printable() {

            public int print(Graphics pg, PageFormat pf, int pageNum) {

                    if (pageNum > 0) {
                            return Printable.NO_SUCH_PAGE;
                    }
                    JLayeredPane j1 = new JLayeredPane();
                    Dimension size = j1.getSize();

                    j1.print(bi.getGraphics());

                    Graphics2D g2 = (Graphics2D) pg;
                    g2.translate(pf.getImageableX(), pf.getImageableY());
                    g2.drawImage(bi, 0, 0, (int) pf.getWidth(), (int) pf.getHeight(), null);

                    return Printable.PAGE_EXISTS;
            }
    };

    Paper paper = new Paper();
    paper.setImageableArea(0, 0, 0, 0);
    paper.setSize(1.15, 0.72);

    PageFormat format = new PageFormat();
    format.setPaper(paper);


    printjob.setPrintable(printable, format);

    try {
            printjob.printDialog();
            printjob.print();

    } catch (Exception eee){
            System.out.println("NO PAGE FOUND."+eee.toString());

    }

I found out that paper.setSize(1.15, 0.7); is in inch (http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/print/Paper.html) paper.setImageableArea(0, 0, 0, 0); and i dont know about this setImageableArea.

does any one has clue about the current sizes, do i made a mistake calculating ? thanks.

Admittedly answered 6/2, 2013 at 9:21 Comment(0)
I
6

First off, "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;
        }

    }

}
Intelligence answered 6/2, 2013 at 10:11 Comment(1)
thank you, thats kind of worked but the resulotion was very poor. i think that the next step will be to check how to print with windows printing dialog. i posted this question btw : #14729627 ... thank you againAdmittedly

© 2022 - 2024 — McMap. All rights reserved.