How to print a long page on physical printer using java?

Viewed 173

I want to print 1000 line(numbered from 1 to 1000) on a virtual printer (Microsoft Print to PDF)but it prints only 122 line not more then this. what I have tried is:
Case 1:

package hindirewr;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import static java.awt.print.Printable.NO_SUCH_PAGE;
import static java.awt.print.Printable.PAGE_EXISTS;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

/**
 *
 * @author Suthar
 */
public class HelloTRY implements  ActionListener{
    
    
 
PrinterJob pj;
    public void actionPerformed(ActionEvent e) {
         pj = PrinterJob.getPrinterJob();        
        pj.setPrintable(new BillPrintable(),getPageFormat(pj));
        try {
            
             pj.print();
             
        }
         catch (PrinterException ex) {
                 JLabel label = new JLabel("Printer Exception error !!! Error0019");
            label.setFont(new Font("Arial", Font.BOLD, 18));
            JOptionPane.showMessageDialog(null,label,"ERROR",JOptionPane.WARNING_MESSAGE);
        }
    }
    
public class BillPrintable implements Printable {
    
   
    
    
  public int print(Graphics graphics, PageFormat pageFormat,int pageIndex) 
  throws PrinterException 
  {    
      
        
      if (pageIndex>0){     //As i wants to print only one page
        return NO_SUCH_PAGE;
    }
      int y=1;
            Graphics2D g2d = (Graphics2D) graphics;                    
            g2d.translate((int) pageFormat.getImageableX(),(int) pageFormat.getImageableY()); 
            g2d.setFont(new Font("Monospaced",Font.PLAIN,9));
            try{
                for(int i=0;i<1001;++i){
                g2d.drawString("Line number "+i+" is printed", 50, y);y+=10;
                }
             return PAGE_EXISTS; 
    }
    catch(Exception r){
      JLabel label = new JLabel("r.printStackTrace  !!! Error0019"+r);
            label.setFont(new Font("Arial", Font.BOLD, 18));
            JOptionPane.showMessageDialog(null,label,"ERROR",JOptionPane.WARNING_MESSAGE);  
    
    }

              return PAGE_EXISTS;    
              
           
      }
   }
private static Paper paper;
PageFormat pf;
public PageFormat getPageFormat(PrinterJob pj)
{
    
    pf = pj.defaultPage();
    paper = pf.getPaper();    
    double width = 250;      //width
    double height = 10000;      //Paper Height = OneLine*NumberOfLine = 10*1000=10000
    paper.setSize(width, height+50);
    paper.setImageableArea(                    
        0,
        0,
        width,            
        height
    );   
            
    pf.setOrientation(PageFormat.PORTRAIT);          
    pf.setPaper(paper);  
    return pf;
}
    public static void main(String args[]) {
 
        UIManager.put("swing.boldMetal", Boolean.FALSE);
        JFrame f = new JFrame("Hello World Printer");
        f.addWindowListener(new WindowAdapter() {
           public void windowClosing(WindowEvent e) {System.exit(0);}
        });
        JButton printButton = new JButton("Print");
        printButton.addActionListener(new HelloTRY());
        f.add("Center", printButton);
        f.pack();
        f.setVisible(true);
    }
    
}

Output for case one:enter image description here ,br>It prints only 122 line

Case 2:

package hindirewr;



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.print.*;
import static java.awt.print.Printable.NO_SUCH_PAGE;
import static java.awt.print.Printable.PAGE_EXISTS;

public class TRY2 implements Printable, ActionListener {


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

        if (page > 0) { /* We have only one page, and 'page' is zero-based */
            return NO_SUCH_PAGE;
        }
int y=1;
        Graphics2D g2d = (Graphics2D)g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());
        g2d.setFont(new Font("Monospaced",Font.PLAIN,9));
        for(int i=1;i<1000;++i){
        g.drawString("Line number "+i+" is printed", 100, y);y+=10;
        }

        return PAGE_EXISTS;
    }

    public void actionPerformed(ActionEvent e) {
         PrinterJob job = PrinterJob.getPrinterJob();
         job.setPrintable(this);
         boolean ok = job.printDialog();
         if (ok) {
             try {
                  job.print();
             } catch (PrinterException ex) {
              /* The job did not successfully complete */
             }
         }
    }

    public static void main(String args[]) {
 
        UIManager.put("swing.boldMetal", Boolean.FALSE);
        JFrame f = new JFrame("Printer");
        f.addWindowListener(new WindowAdapter() {
           public void windowClosing(WindowEvent e) {System.exit(0);}
        });
        JButton printButton = new JButton("Print2");
        printButton.addActionListener(new TRY2());
        f.add("Center", printButton);
        f.pack();
        f.setVisible(true);
    }
}


Output: enter image description here


It prints Only 101 Lines

How I can Print 1000 Line on a page roll?

0 Answers
Related