【问题标题】:Printing on a thermal printer Java在热敏打印机 Java 上打印
【发布时间】:2013-07-31 17:55:45
【问题描述】:

我正在使用以下代码在带有 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();

知道为什么会发生这种情况以及如何解决吗?

【问题讨论】:

  • 我遇到了完全相同的问题,但没有得到答案。它是光栅 rnp 打印机吗?
  • 没有。这是爱普生 TM-T81。
  • 即便如此尝试使用我的代码,我有一个在普通打印机上没有打印对话框的情况下工作,但它在我的打印机上不起作用,因为它是一台 ptr 打印机。希望它有效。 stackoverflow.com/questions/17724784/…

标签: java printing


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2012-10-05
    • 1970-01-01
    • 2019-12-06
    • 2019-03-15
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    相关资源
    最近更新 更多