2016-12-14 97 views
0

我的java熱敏打印機代碼無法打印超長收據(超過A4紙張尺寸)。它的工作正常,但是如果購物車中的物品太多,則會生成一半的打印件。我的代碼是在mentioned-Java代碼無法在熱敏打印機上打印超長收據

public PrintReceipt(Map<String,String> hm){ 

     /* 
      product details code.. 


     */ 


     try{ 


      input = new FileInputStream("C:/printer.properties"); 
      prop.load(input); 
      printerName=prop.getProperty("receiptPrinter"); 
      System.out.println("Printer Name "+printerName); 

       }catch(Exception exception){ 
        System.out.println("Properties file not found"); 
       } 

      PrintService[] pservices = PrintServiceLookup.lookupPrintServices(null,null); 

      for (int i = 0; i < pservices.length; i++) { 
      if (pservices[i].getName().equalsIgnoreCase(printerName)) { 
      job = PrinterJob.getPrinterJob(); 
      PageFormat pf = job.defaultPage(); 

      double margin = 1.0D; 
      Paper paper = new Paper(); 
      paper.setSize(216D, paper.getHeight()); 
      paper.setImageableArea(margin, margin, paper.getWidth() - margin * 1.5D, paper.getHeight() - margin * 1.5D); 
      pf.setPaper(paper);   
      job.setCopies(1); 
      pf.setOrientation(1); 
      job.setPrintable(this, pf); 

      try 
      { 
       job.print(); 
      } 
      catch(PrinterException ex) 
      { 
      System.out.println("Printing failed"); 

     } 
     } 
    } 
} 



public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) 
     throws PrinterException { 
    if(pageIndex > 0) 
     return 1; 


      Graphics2D g2d = (Graphics2D)graphics; 


      double width = pageFormat.getImageableWidth(); 
      double height = pageFormat.getImageableHeight();  
      g2d.translate((int) pageFormat.getImageableX(),(int) pageFormat.getImageableY()); 
      Font font = new Font("Monospaced",Font.BOLD,8);  
      g2d.setFont(font); 

      try { 
       /* 
         * Draw Image* 


         */ 
            int x=50 ;          
            int y=10;          
            int imagewidth=100; 
            int imageheight=50; 
          BufferedImage read = ImageIO.read(new File("C:/hotel.png")); 
          g2d.drawImage(read,x,y,imagewidth,imageheight,null);   //draw image 
          g2d.drawString("-- * Resturant * --", 20,y+60); 
          g2d.drawLine(10, y+70, 180, y+70);       //draw line 
           } catch (IOException e) { 
         e.printStackTrace(); 
        } 
      try{ 

       /*Draw Header*/ 


     /* 
      product details code.. 


     */ 


      /*Footer*/ 


      //end of the receipt 



      } 
      catch(Exception r){ 
       r.printStackTrace(); 
      } 


    return 0; 
} 

請讓我知道我可以通過糾正我的代碼生成較長的收據打印,或者如果您有任何更好的辦法做到這一點。

+0

是一個API的'Paper'類部分還是你實現它? – CraigR8806

+0

其部分API java.awt.print.Paper – 007

回答

0

就在這裏:

Paper paper = new Paper(); 
paper.setSize(216D, paper.getHeight()); 

您正在創建一個新的Paper對象,而不是設置其高度。

這是一個link這個類的文檔。

當創建一個文件對象,它是應用程序的責任,以確保紙張大小和可成像區域兼容

你需要設置紙張的高度通過調用paper.setSize(width, height),否則將使用其默認大小屬性。

尺寸以1/72英寸提供。

所以寬度和高度都需要以這種格式被設置爲double小號

+0

如果此解決方案適合您,請將其選爲答案。 – CraigR8806

+0

我知道關於紙類方法,我已經通過給定義的高度和寬度來嘗試它。但它不適合我。 – 007

相關問題