【问题标题】:how to print report in fit to billing printer in java如何在java中打印适合计费打印机的报告
【发布时间】:2017-09-17 01:06:20
【问题描述】:

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

 @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 报告来打印它我 jsut 直接打印我的 jframe 帮助? 提前谢谢

【问题讨论】:

标签: java swing printing


【解决方案1】:

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

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

但本质上是在你的 try 块周围添加:

if (pj.printDialog()) 

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

【讨论】:

  • 我阅读了所有答案,但我发现最有用的是我认为 pj.printdialog() 。但是有一件事仍然不清楚,如何说我的打印机在纸卷上打印调整 bcz 我在纸卷上打印,纸张尺寸是字母,没有任何纸卷或诸如纸卷之类的下拉,所以我需要知道如何根据纸卷调整打印
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-05
  • 1970-01-01
  • 2019-03-15
  • 1970-01-01
  • 1970-01-01
  • 2018-08-07
相关资源
最近更新 更多