2

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?

Suthar
  • 91
  • 2
  • 8
  • I have Updated Question with full information – Suthar Sep 25 '20 at 17:24
  • You're going to have to divide your output lines into pages. 50 lines per page would be appropriate. You'll have to calculate the page height so that your pages don't leave gaps. – Gilbert Le Blanc Sep 25 '20 at 18:57
  • You mean that there is upper limit on height of Page? As in Question I am giving a perfect height that it required (`double width = 250; //width double height = 10000; //Paper Height = OneLine*NumberOfLine = 10*1000=10000 paper.setSize(width, height+50); `)
    But doesn't work as it is suppose to.
    – Suthar Sep 26 '20 at 02:37
  • Look at previous comment . @GilbertLeBlanc – Suthar Sep 26 '20 at 02:44
  • Yes, there's an upper limit on the height and width of a page. – Gilbert Le Blanc Sep 26 '20 at 02:48
  • When I am going for a shorter page like Height =200; and other thing are same the printer create and print(on Physical Printer) a Full Size A4 page.It doesn't stop after height of 200.the Bottom portion (A4 height - 200) print a blank page but i don't want that blank footer.How i can print that much exactly length only @GilbertLeBlanc – Suthar Sep 26 '20 at 02:57

0 Answers0