2016-03-04 52 views
0

我试图打印一个连续的缓冲图像到单独的页面上。我遇到的问题是,我能做的最好的是打印最后一页。变量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(); 
} 
+0

你可以显示你在哪里调用printImage()方法。 – user3437460

+0

提示:**从不**有空的catch子句;至少做一些伐木工作;或打印堆栈跟踪;所以当你测试你的东西时,你确定你没有忽略任何异常。 **从不**在评论中提供更多信息。请更新您的问题。并表现出更多的尊重:花时间将源代码格式化为可读。你希望别人花时间帮助你。所以你花了一些时间尽可能简单。 – GhostCat

+0

你实际上可以编辑这个问题,把所有的变化都加入到它里面 – Gimby

回答

0

的问题是,INTY是每个print方法在内部调用时递增。如果每个页面只调用一次print方法,那么这将起作用。

java.awt.print.Printablehttps://docs.oracle.com/javase/7/docs/api/java/awt/print/Printable.html)的的JavaDoc:

打印系统可以请求页面索引超过一次。每次都会提供相同的PageFormat参数。

而是统计了当前页号本身,您的打印方法应该使用page参数来确定要打印的图像的一部分。

public class PrintDocs implements Printable { 
    private static final int pageHeight = 842; 
    private BufferedImage incomingImage; 
    private int pagePrintCount = 0; 

    public PrintDocs(BufferedImage incomingImage) { 
     this.incomingImage = incomingImage; 
    } 

    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); 
      } catch (PrinterException ex) { 
       System.out.println("The job did not successfully complete"); 
       ex.printStackTrace(); 
      } 

     } 

    } 

    public int print(Graphics g, PageFormat pf, int page) throws PrinterException { 

     System.out.println("print called for page="+page+", call count="+pagePrintCount++); 

     /* 
     * 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 (page*pageHeight < 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, pageHeight, 0, page*pageHeight, 595, pageHeight*(page+1), null); 

      return PAGE_EXISTS; 
     } 
     return NO_SUCH_PAGE; 
    } 
} 
1

你的变量inty似乎重置因为你总是创建每次您的actionPerformed方法被调用PrintDocs的新实例。

PrintDocs sendToPrinter = new PrintDocs(printCopy); //This line causing the reset of inty 
sendToPrinter.PrintImage(); 

为了防止这种情况,你可以保持相同的PrintDocs实例的引用:

private void printActionPerformed(java.awt.event.ActionEvent evt){ 
    BufferedImage printCopy = tooOutput.getPrintImage(); 
    sendToPrinter.PrintImage(); //Keep a reference of sendToPrinter in your class 
} 

另一种解决方案将作出intY一个staic变量和printImage()的静态方法。当你需要print,只需通过类名调用:

PrintDos.printImage(); 

你的类将是这样的:

public class PrintDocs implements Printable { 
    static int inty = 0; //<= become static 

    //other variables, methods and constructor not shown 

    public static void PrintImage() { //<= become static 
     //implementation for printImage 
    } 
} 
+0

对不起,我仍然试图让我的头在附近。如果我使用静态方法,我如何向下移动缓冲的图像以打印整个文档? – Gerry

+0

@Gerry通过向下移动缓冲的图像意味着什么? – user3437460

+0

我的形象是漫长而连续的。每次循环操作inty改变值给我我的页面大小。 – Gerry