title: Printing with a continuous paper on Dot Matrix Printer without page break using JAVA
body: <p>This is my code to get user's account number and set it to a paper to print.</p>
  
  <pre><code>import java.awt.print.PageFormat;
  import java.awt.print.Paper;
  import java.awt.print.PrinterException;
  import java.awt.print.PrinterJob;
  import javax.swing.JOptionPane;
  
  
  
  /**
   *
   * @author Govinda
   */
  public class ChequePrint{
  
      public static void main(String[] args) {
  
          // variable defnations
  
          String accNum;
          Long cheqNum;
          String solName;
          int qtyCheq;
          String holderName;
  
          //getting account number from user
          accNum = JOptionPane.showInputDialog(null, "Enter Account Number:", "Cheque Print",
          JOptionPane.QUESTION_MESSAGE);
  
          //setting default values to test
          cheqNum = 12345678912l;
          qtyCheq = 5;
          holderName = "Govinda Thapa";
          solName="Thapathali";
  
          //generating cheque
          GetCheque newCheque = new GetCheque();
  
          //setting printing values on cheque
  
          newCheque.setAccountHolderName(holderName);
          newCheque.setAccountNumber(accNum);
          newCheque.setNumberOfCheque(qtyCheq);
          newCheque.setChequeNumber(cheqNum);
          newCheque.setBranchName(solName);
  
  
  
          //Now setting paper and printer job
          Double width = 8.5 * 72.0;
          Double height =  4.5 * 72.0 * qtyCheq;
          double margin = 0;
          Paper paper = new Paper();
          paper.setSize(width, height);
          paper.setImageableArea(margin, margin, width - (margin * 2), height - (margin * 2));
          paper.setImageableArea(0, 0, width, height);
          PageFormat pf = new PageFormat();
          //Paper paper = pf.getPaper();
          pf.setPaper(paper);
          pf.setOrientation(PageFormat.PORTRAIT);
          PrinterJob job = PrinterJob.getPrinterJob();
          job.setPrintable(newCheque,pf);
  
          try {
              job.print();
          }
          catch(PrinterException pe) {
              System.out.println("error Occured" + pe.getMessage());
          }
  
  
  
      }
  
  }
  </code></pre>
  
  <p>This is my print function that print the required number of cheque.</p>
  
  <pre><code>import java.awt.Font;
  import java.awt.Graphics;
  import java.awt.Graphics2D;
  import java.awt.print.PageFormat;
  import java.awt.print.Printable;
  import java.awt.print.PrinterException;
  
  /*
   * To change this license header, choose License Headers in Project Properties.
   * To change this template file, choose Tools | Templates
   * and open the template in the editor.
   */
  
  /**
   *
   * @author Govinda
   */
  public class GetCheque implements Printable{
  
      // variable defnations
      //
      private String accountNumber;
      private long chequeNumber;
      private String branchName;
      private int numberOfCheque;
      private String accountHolderName;
  
      public GetCheque(){
  
          /*    this.accountHolderName="";
          this.accountNumber="";
          this.branchName="";
          this.numberOfCheque=0;
          this.chequeNumber=0l;*/
  
  
      }
      public String getAccountNumber() {
          return accountNumber;
      }
  
      public void setAccountNumber(String accountNumber) {
          this.accountNumber = accountNumber;
      }
  
      public long getChequeNumber() {
          return chequeNumber;
      }
  
      public void setChequeNumber(long chequeNumber) {
          this.chequeNumber = chequeNumber;
      }
  
      public String getBranchName() {
          return branchName;
      }
  
      public void setBranchName(String branchName) {
          this.branchName = branchName;
      }
  
      public int getNumberOfCheque() {
          return numberOfCheque;
      }
  
      public void setNumberOfCheque(int numberOfCheque) {
          this.numberOfCheque = numberOfCheque;
      }
  
      public String getAccountHolderName() {
          return accountHolderName;
      }
  
      public void setAccountHolderName(String accountHolderName) {
          this.accountHolderName = accountHolderName;
      }
  
      //printing preferences
      private double xCoor = 0;
      private double yCoor = 0;
      private double margin = 0.25 * 72.0;
      private double marginX = 0.5 * 72.0;
      private Font mainFont = new Font("Roman PS", Font.PLAIN, 9);    
  
  
      // Print method to print 
  
      @Override
      public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
  
          if(pageIndex &gt; 0) {
              return Printable.NO_SUCH_PAGE;
          }
  
  
          //graphics.setFont(mainFont);
          //int charHeight = graphics.getFontMetrics().getHeight();
  
          /*        //graphics.translate(0,0);
          //for(int i=0;i&lt;numberOfCheque;i++) {
          graphics.drawString(String.valueOf(chequeNumber), (int )(xCoor + marginX), (int)(yCoor + margin));
          yCoor=yCoor+(4*charHeight);
          yCoor=yCoor+charHeight;
          graphics.drawString(branchName, (int)(xCoor + marginX), (int)(yCoor + margin));
          yCoor=yCoor+charHeight;
          graphics.drawString(accountNumber, (int)(xCoor + marginX), (int)(yCoor + margin));
          yCoor=yCoor+charHeight;
          graphics.drawString(accountHolderName, (int)(xCoor + marginX), (int)(yCoor + margin));
          yCoor=yCoor+(3*charHeight);
          chequeNumber = chequeNumber + 1;
  
          //}*/
  
  
          Graphics2D g2d = (Graphics2D)graphics;
          g2d.setFont(mainFont);
  
          int charHeight = g2d.getFontMetrics().getHeight();
  
          // Perform rendering
          g2d.translate(0f,0f);
  
          for(int i=0;i&lt;numberOfCheque;i++) {
  
          g2d.drawString(String.valueOf(chequeNumber), (float)(xCoor + marginX), (float)(yCoor + margin));
          yCoor=yCoor+(4*charHeight);
          yCoor=yCoor+charHeight;
          g2d.drawString(branchName, (float)(xCoor + marginX), (float)(yCoor + margin));
          yCoor=yCoor+charHeight;
          g2d.drawString(accountNumber, (float)(xCoor + marginX), (float)(yCoor + margin));
          yCoor=yCoor+charHeight;
          g2d.drawString(accountHolderName, (float)(xCoor + marginX), (float)(yCoor + margin));
          yCoor=yCoor+(3*charHeight);
          chequeNumber = chequeNumber + 1;
  
          }
  
          return 0;
      }
  
  
  }
  </code></pre>
  
  <p>I set the height of page with relative to the number of cheque I hvae to print. But it did not work. What I have to do to print the desired number of cheque in a continuous paper on DOT Matrix Printer. </p>
  
  <p>This code can print upto 5 cheques normally(print start from half of the page) , but if I set the qtyCheque to higher number like 10 or 15 then it doesn't print at all. </p>
  
  <p>What should I do to start the print from the begining of the paper and set the paper height with the relative to the number of cheques to print?</p>
  
  <p><em>Thanks for your answer, but how to write my own printer driver, and     how to install it against its original driver? May I have to  install    it as different component or it can be within my code? I just need to    print text and numbers which should be support backslash characters      like \n, \t etc.  Now can you help me? I already tried the given       options but the problem is variable chequeNumber is increased without    printing and then it will print the content with increased       chequeNumber. So I would like to try your another idea i.e, making       driver for the printer. What should I have to do to make printer       driver for myself? Please explain it in detail if possible.</em></p>
  
  <p>*</p>
  
  <p>*</p>