我正在尝试实现可打印接口以打印收据。以下代码正在运行,但问题是在所需收据的顶部添加了一张白纸。为什么要打印两页而不是一页(空白纸是 A4 尺寸)?
package slg.stock.util.print;
public class ForumPost implements Printable {
private Paper receiptPaper;
private double paperWidth = 2.2;
private double paperHeight = 7;
double leftMargin = 0.05;
double rightMargin = 0.1;
double topMargin = 0.4;
double bottomMargin = 0.1;
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageIndex > 1) {
return Printable.NO_SUCH_PAGE;
}
receiptPaper = new Paper();
receiptPaper.setSize(paperWidth * 72.0, paperHeight * 72.0);
receiptPaper.setImageableArea(leftMargin * 72.0, topMargin * 72.0,
(paperWidth - leftMargin - rightMargin) * 72.0,
(paperHeight - topMargin - bottomMargin) * 72.0);
pageFormat.setPaper(receiptPaper);
Graphics2D g2d = (Graphics2D) graphics;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
g2d.drawString("helo \n"
+ new Date()+ " ", 5, 5);
return Printable.PAGE_EXISTS;
}
public static void main(String args[]) {
try {
PrinterJob job = PrinterJob.getPrinterJob();
ForumPost printer = new ForumPost();
job.setPrintable(printer);
job.print();
} catch (PrinterException ex) {
}
}
}
我不熟悉打印 API。