2013-07-31 165 views
2

我用下面的代码来打印80毫米卷纸热敏打印机上的一些文字上:打印的热敏打印机的Java

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

任何想法为什么会发生这种情况,它如何解决?

+0

我有完全相同的问题,没有得到答案。它是一个光栅rnp打印机? –

+0

不是。这是Epson TM-T81。 –

+0

即使如此尝试使用我的代码我有一个工作没有打印对话框到普通打印机,但它没有在我的工作,因为它是一个ptr打印机。希望它有效。 http://stackoverflow.com/questions/17724784/printing-files-in-java-with-raster-ptr-printer –

回答

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

在前面的源代码(错误的),当应用程序触发打印对话框,用户点击OK,默认的打印首选项转移到Java应用程序,它打印什么是必需的。但是当我们通过删除这一行来禁用打印对话框时: boolean don = job.printDialog();

一个不明的PageFormat被传它产生突然冒出。问题是不符合我们定义的PageFormat,问题是,PageFormat中被传递到我们没有做最初的印刷方法:

job.setPrintable(contentToPrint, pageFormat); 

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