ArrayPrinter

The ArrayPrinter class will send an ARRAY of Strings to a printer.
To use it, compile it into the application folder, then issue the command:

   new ArrayPrinter(INFO);

where INFO is a String[] array.  A standard PrinterChoosing dialog will pop up,
and then the document will be printed.

Click here to download the .java file:  ArrayPrinter.java

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Properties;

public class ArrayPrinter extends Frame
{
   // This prints an ARRAY of Strings, one on each line of the paper.
   
   private Properties p = new Properties();
   public static void main(String[] args)
   {
      String[] info = {"print", "this", "to", "the printer"};
      new ArrayPrinter(info);
   }
   
    public ArrayPrinter(String[] info) {
      PrintJob pjob = getToolkit().getPrintJob(this, "Cool Stuff", p);
      if (pjob != null) {
        Graphics pg = pjob.getGraphics();
        if (pg != null) {
          String s = "";
          for (int x =0; x < info.length; x++)
          { s = s + info[x] + "\n"; }
          printLongString (pjob, pg, s);
          pg.dispose();
        }
        pjob.end();
      }
      dispose();
    }
 
  // I'm assuming a one-inch margin on all
  // four sides. This could be done better.
  private int margin = 72;
  
  // Print string to graphics via printjob
  // Does not deal with word wrap or tabs
  private void printLongString (PrintJob pjob, Graphics pg, String s) {
      
    int pageNum = 1;
    int linesForThisPage = 0;
    int linesForThisJob = 0;
    // Note: String is immutable so won't change while printing.
    if (!(pg instanceof PrintGraphics)) {
      throw new IllegalArgumentException ("Graphics context not PrintGraphics");
    }
    StringReader sr = new StringReader (s);
    LineNumberReader lnr = new LineNumberReader (sr);
    String nextLine;
    int pageHeight = pjob.getPageDimension().height - margin;
    Font helv = new Font("Helvetica", Font.PLAIN, 12);
    //have to set the font to get any output
    pg.setFont (helv);
    FontMetrics fm = pg.getFontMetrics(helv);
    int fontHeight = fm.getHeight();
    int fontDescent = fm.getDescent();
    int curHeight = margin;
    try {
      do {
        nextLine = lnr.readLine();
        if (nextLine != null) {         
          if ((curHeight + fontHeight) > pageHeight) {
            // New Page
            System.out.println ("" + linesForThisPage + " lines printed for page " + pageNum);
            if (linesForThisPage == 0) {
               System.out.println ("Font is too big for pages of this size; aborting...");
               break;
            }
            pageNum++;
            linesForThisPage = 0;
            pg.dispose();
            pg = pjob.getGraphics();
            if (pg != null) {
              pg.setFont (helv);
            }
            curHeight = 0;
          }
          curHeight += fontHeight;
          if (pg != null) {
            pg.drawString (nextLine, margin, curHeight - fontDescent);
            linesForThisPage++;
 
            linesForThisJob++;
          } else {
            System.out.println ("pg null");
          }
        }
      } while (nextLine != null);
    } catch (EOFException eof) {
      // Fine, ignore
    } catch (Throwable t) { // Anything else
      t.printStackTrace();
    }
    System.out.println ("" + linesForThisPage + " lines printed for page " + pageNum);
    System.out.println ("pages printed: " + pageNum);
    System.out.println ("total lines printed: " + linesForThisJob);
  }
  
}