嘿有即时制作发票软件一切都很好只是问题是当我的报告打印时这是根据标准打印机打印,但我想通过帐单收据打印它打印机如何在java中打印适合帐单打印机的报告
@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);
}
//}
//}
}
}
此代码打印我的收据,但这是印刷根据标准的打印机,当我想通过结算打印机 进行打印并使用加斯帕报告打印出来IM仅仅指刚林不直接打印我的JFrame 帮帮我? thankx提前
有很多方法可以做到这一点。如果你知道收据打印纸的大小,你可以使用[this example](https://stackoverflow.com/questions/11803741/printing-in-java-to-label-printer/11805237#11805237)来改变纸张尺寸 – MadProgrammer
[这是另一个例子](https://stackoverflow.com/questions/28427566/save-jpanel-as-image-hd-quality/28492187#28492187)在同一个想法和[这个例子也是如此](https://stackoverflow.com/questions/27029166/java-printerjob-not-printing-to-fit-paper/27029220#27029220) - 我“认为”你可能有的问题是,知道“高度”的页面,如果它是一个热敏式打印机,只是有一个滚动 – MadProgrammer
如果您使用的是热敏打印机,您可能需要[看看这些其他问题](https://stackoverflow.com/search?q= %5Bjava%5D +热+打印机)作为一些(我知道),有自己的打印机API – MadProgrammer