如何在java中打印适合计费打印机的报表

时间:2017-09-16 13:14:13

标签: java swing printing

嘿我在制作发票软件一切都很好只是问题是当我的报告打印时这是根据标准打印机打印但我想通过账单收据打印机打印

 @Override
   public int print(Graphics g, PageFormat format, int page_index) 
    throws PrinterException {
if (page_index > 0) {
    return Printable.NO_SUCH_PAGE;
}

// get the bounds of the component
Dimension dim = comp.getSize();
double cHeight = dim.getHeight();
double cWidth = dim.getWidth();

// get the bounds of the printable area
double pHeight = format.getImageableHeight();
double pWidth = format.getImageableWidth();

double pXStart = format.getImageableX();
double pYStart = format.getImageableY();

double xRatio = pWidth / cWidth;
double yRatio = pHeight / cHeight;


Graphics2D g2 = (Graphics2D) g;
g2.translate(pXStart, pYStart);
g2.scale(xRatio, yRatio);
comp.paint(g2);

return Printable.PAGE_EXISTS;
}




 public static void printing(){









  PrinterJob pjob = PrinterJob.getPrinterJob();
  PageFormat preformat = pjob.defaultPage();
  preformat.setOrientation(PageFormat.PORTRAIT);
  PageFormat postformat = pjob.defaultPage();
 //If user does not hit cancel then print.
 if (preformat != postformat) {
  //Set print component
   pjob.setPrintable(new reporting(frame), postformat);
  //  if (pjob.printDialog()) {
   try {
    pjob.print();
    frame.dispose();
    } catch (PrinterException ex) {
    Logger.getLogger(reporting.class.getName()).log(Level.SEVERE, null, ex);
   }
 //}
//}
}

} 

此代码正在打印我的收据,但这是根据标准打印机打印,因为我想通过计费打印机打印 并且我没有使用jesper报告来打印它直接打印我的jframe 救命? 谢谢你提前

1 个答案:

答案 0 :(得分:0)

如果您正在寻找用户通过输入指定打印机,我尝试使用系统打印对话框,更多详细信息:

https://docs.oracle.com/javase/tutorial/2d/printing/dialog.html

但实质上是在你的try块附近添加:

if (pj.printDialog()) 

如果您正在寻找的是一种更具编程性的方式将作业发送到指定的打印机,并且您提前知道打印机的名称,那么您可以使用打印服务。也许following SO question可能有用。