我试图打印一个连续的缓冲图像到单独的页面上。我遇到的问题是,我能做的最好的是打印最后一页。变量inty的计数似乎被忽略,直到它结束。 这里是我的代码打印一个连续的BufferedImage
public class PrintDocs implements Printable {
BufferedImage incomingImage;
int inty;
public PrintDocs(BufferedImage incomingImage) {
this.incomingImage = incomingImage;
inty = 0;
}
public void PrintImage() {
HashPrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
attr.add(new MediaPrintableArea(0f, 0f, incomingImage.getWidth()/72f, incomingImage.getHeight()/72f,
MediaPrintableArea.INCH));
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
PageFormat pf = job.pageDialog(job.defaultPage());
boolean ok = job.printDialog();
if (ok) {
try {
job.print(attr);
inty = inty + 842;
} catch (PrinterException ex) {
/* The job did not successfully complete */
}
}
}
public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
// if (page > 0){ //Use this for the moment may get rid of it
// return NO_SUCH_PAGE;
// }
/*
* User (0,0 is typacally outside the imageable area, so we must
* translate by the X and Y values in the PageFormatz to avoid clipping
*/
if (inty < incomingImage.getHeight()) {
// if (inty < incomingImage.getHeight()){
Graphics2D g2d = (Graphics2D) g;
g2d.translate(pf.getImageableX() - 1.0, pf.getImageableY() - 1.0);
/* Now we perform out rendering */
g.drawImage(incomingImage, 0, 0, 595, 842, 0, inty, 595, 842 + inty, null);
// g2d.drawImage(incomingImage, 0, 0, 595, 842, 0, inty, 595,842 +
// inty, null);
System.out.println("inty = " + inty);
inty = inty + 842;
return PAGE_EXISTS;
}
return NO_SUCH_PAGE;
}
}
调用printImage():
private void printActionPerformed(java.awt.event.ActionEvent evt){
// TODO add your handling code here:
//BufferedImage printCopy = tooOutput.displayProcessing.getCreatedImage();
BufferedImage printCopy = tooOutput.getPrintImage();
PrintDocs sendToPrinter = new PrintDocs(printCopy);
sendToPrinter.PrintImage();
}
你可以显示你在哪里调用printImage()方法。 – user3437460
提示:**从不**有空的catch子句;至少做一些伐木工作;或打印堆栈跟踪;所以当你测试你的东西时,你确定你没有忽略任何异常。 **从不**在评论中提供更多信息。请更新您的问题。并表现出更多的尊重:花时间将源代码格式化为可读。你希望别人花时间帮助你。所以你花了一些时间尽可能简单。 – GhostCat
你实际上可以编辑这个问题,把所有的变化都加入到它里面 – Gimby