如何在此示例中禁止打印对话框?

时间:2010-06-23 23:52:15

标签: java printing dialog

private void printCard() {

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

        Printable printable = new Printable() {

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

                        if (pageNum > 0) {
                                return Printable.NO_SUCH_PAGE;
                        }

                        Dimension size = jLayeredPane2.getSize();
                        BufferedImage bufferedImage = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);

                        jLayeredPane2.print(bufferedImage.getGraphics());

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

                        return Printable.PAGE_EXISTS;
                }
        };

        Paper paper = new Paper();
        paper.setImageableArea(0, 0, 153, 243);
        paper.setSize(243, 154);

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

        printjob.setPrintable(printable, format);
        if (printjob.printDialog() == false)
                return;

        try {
                printjob.print();
        } catch (PrinterException ex) {
                System.out.println("NO PAGE FOUND." + ex);

        }
}

1 个答案:

答案 0 :(得分:1)

从我在您的代码中看到的内容,您致电if (printjob.printDialog() == false)。这将始终尝试显示本机打印机属性对话框。 boolean返回值取决于用户是单击“确定”还是取消退出对话框。如果要取消隐藏对话框,请删除该if块,因为您要执行的打印工作是通过printjob.print()调用完成的。