4

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

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();

关于为什么会发生这种情况以及如何解决的任何想法?

4

1 回答 1

2

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

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);

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

于 2013-08-01T15:52:55.287 回答