0 Replies Latest reply: Apr 9, 2013 6:09 AM by 905809 RSS

    printing with java app

    905809
      Hi all, I'm making a program for printing on continuous paper.
      I have 2 steps to my program so far and issues in all of them:

      1) Creating the print job, setting the paper size, margins, sending to printer
      2) I'm printing to a file, so I can remove the last 4 byets of the file that make the printer scroll the page out.


      The issues in the first part is that I can't seem to start printing at the top of the page (it always scrolls down like 2 inches). Changing the imageable area and margins don't seem to help with that or I just don't understand them. If I set margins to 0, it's just clipping the text and not printing it out properly. I also can't print more than 1 page size (height-wise), which sucks, considering I'm printing on continious paper.
       
          @Override
          public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
              if (page > 0) {
                  return NO_SUCH_PAGE;
              }
      
              Paper paper = new Paper();
              paper.setSize(612, 10000);
              double margin = 72;
              paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight()
              - margin * 2);
              pf.setPaper(paper);
              
              Graphics2D g2d = (Graphics2D)g;
              g2d.translate(pf.getImageableX(), pf.getImageableY());
              
              
              Font big = new Font("Times New Roman", Font.PLAIN, 12);
              Font small = new Font("Times New Roman", Font.PLAIN, 8);
              int Xlocation = 20;
              int y = 20;
              int count = 20;
              int i = 1;
              
              
              while (i < count){
                  g2d.setFont(small);
                  g.drawString("1", Xlocation , y);
                  y += 8;
                  g.drawString("2", Xlocation , y);
                  y+=12;
                  g2d.setFont(big);
                  g.drawString("3", Xlocation , y);
                  y += 8;
                  g2d.setFont(small);
                  g.drawString("4", Xlocation , y);
                  y += 20;
                  i++;
              }
      
              // tell the caller that this page is part of the printed document
              return PAGE_EXISTS;
          }
      In the second part I'm removing the last 4 bytes of the file and adding a new line so it wouldn't scroll the page out after it is done printing. This works fine.
      I would like it to not prompt the user for file name, but give it a default name of something like "print.prn" and then run command prompt and enter command "copy /b print1.prn lpt1" which would send it to printer and then close command prompt. Is this possible?

      the second part:
              try {
                  File target = new File(filename);
                  RandomAccessFile file = new RandomAccessFile(target,"rw");
                  file.setLength(target.length()-4); // Strip off the last 4bytes
                  file.seek(target.length()); // set pointer to the end of file
                  file.writeUTF("\n"); // add new line at the end of file
                  file.close();
                  
                  Runtime rt = Runtime.getRuntime();
                  rt.exec("cmd.exe /c start copy /b " +filename+ " lpt1");
              } catch (FileNotFoundException ex) {
                  Logger.getLogger(Printing.class.getName()).log(Level.SEVERE, null, ex);
              }