【发布时间】: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 帮助? 提前谢谢
【问题讨论】:
-
有很多方法可以做到这一点。如果您知道收据打印纸的尺寸,您可以使用this example 之类的东西来更改纸张尺寸
-
This is another example 和so is this example 的想法相同 - 我“认为”您可能遇到的问题是,如果它是只有一个滚动
-
如果您使用的是热敏打印机,您可能希望 have a look at these other questions 一些(据我所知)拥有自己的打印机 API