How would I suppress the print dialog in this example?

馋奶兔 提交于 2020-02-23 06:46:46

问题


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:


From what I see in your code, you call if (printjob.printDialog() == false). This will always try to show the native printer properties dialog. The boolean return value is based on whether the user clicks OK or cancels out of the dialog. If you want to suppress the dialog, remove that if block, as the printing work that you want to perform is done via the printjob.print() call.



来源:https://stackoverflow.com/questions/3106379/how-would-i-suppress-the-print-dialog-in-this-example

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