在热敏打印机Java上打印

时间:2013-07-31 17:55:45

标签: java printing

我正在使用以下代码在带有80mm卷纸的热敏打印机上打印一些文本:

public class printnow {

    public static void printCard(final String bill) {
        final PrinterJob job = PrinterJob.getPrinterJob();

        Printable contentToPrint = new Printable() {
            @Override
            public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws  PrinterException {
                Graphics2D g2d = (Graphics2D) graphics;
                g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
                g2d.setFont(new Font("Monospaced", Font.BOLD, 7));
                pageFormat.setOrientation(PageFormat.PORTRAIT);

                Paper pPaper = pageFormat.getPaper();
                pPaper.setImageableArea(0, 0, pPaper.getWidth() , pPaper.getHeight() -2);
                pageFormat.setPaper(pPaper);

                if (pageIndex > 0) 
                    return NO_SUCH_PAGE; //Only one page

                String Bill [] = bill.split(";");
                int y = 0;
                for (int i = 0; i < Bill.length; i++) {
                    g2d.drawString(Bill[i], 0, y);
                    y = y + 15;
                }

                return PAGE_EXISTS;
            }
        };  

        boolean don = job.printDialog();

        job.setPrintable(contentToPrint);

        try {
            job.print();
        } catch (PrinterException e) {
            System.err.println(e.getMessage());
        }
    }
}

打印非常精细,正是我想要的。但是,当我删除以下行以禁用打印对话框并自动执行打印过程时,我的打印会混乱,打印机会自动占用左边的边距。

boolean don = job.printDialog();

知道为什么会这样,以及如何解决?

1 个答案:

答案 0 :(得分:0)

经过大量研究和应用一点脑,我找到了解决方案。这是一个非常小但很愚蠢的错误。阅读以下源代码:

public class printnow {

    public static void printCard(final String bill ) {
        final PrinterJob job = PrinterJob.getPrinterJob();

        Printable contentToPrint = new Printable() {
            @Override
            public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
                Graphics2D g2d = (Graphics2D) graphics;
                g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
                g2d.setFont(new Font("Monospaced", Font.BOLD, 7));

                if (pageIndex > 0) {
                    return NO_SUCH_PAGE;
                } //Only one page

                String Bill [] = bill.split(";");
                int y = 0;
                for (int i = 0; i < Bill.length; i++) {
                    g2d.drawString(Bill[i], 0, y);
                    y = y + 15;
                }

                return PAGE_EXISTS;
            }
        };

        PageFormat pageFormat = new PageFormat();
        pageFormat.setOrientation(PageFormat.PORTRAIT);

        Paper pPaper = pageFormat.getPaper();
        pPaper.setImageableArea(0, 0, pPaper.getWidth() , pPaper.getHeight() -2);
        pageFormat.setPaper(pPaper);

        job.setPrintable(contentToPrint, pageFormat);

        try {
            job.print();
        } catch (PrinterException e) {
            System.err.println(e.getMessage());
        }
    }
}

在之前的源代码(错误的代码)中,当应用程序触发打印对话框并且用户单击“确定”时,默认打印首选项将传输到Java应用程序并打印所需内容。但是当我们通过删除这一行来禁用打印对话框时:      boolean don = job.printDialog();

传输未知的PageFormat,这是无处不在的。问题不在于我们定义的PageFormat,问题是pageFormat被传递给我们最初没有做的打印方法:

job.setPrintable(contentToPrint, pageFormat);

注意第二个参数传递给上面的方法。 希望这有助于每个人。 :)