Pageable Text : Print « 2D Graphics GUI « Java

Pageable Text

Pageable Text
 * This example is from the book "Java Foundation Classes in a Nutshell".
 * Written by David Flanagan. Copyright (c) 1999 by O'Reilly & Associates.  
 * You may distribute this source code for non-commercial purposes only.
 * You may study, modify, and use this example for any purpose, as long as
 * this notice is retained.  Note that this example is provided "as is",
 * WITHOUT WARRANTY of any kind either expressed or implied.

import java.awt.*;
import java.awt.print.*;
import java.util.Vector;

public class PageableText implements Pageable, Printable {
  // Constants for font name, size, style and line spacing
  public static String FONTFAMILY = "Monospaced";
  public static int FONTSIZE = 10;
  public static int FONTSTYLE = Font.PLAIN;
  public static float LINESPACEFACTOR = 1.1f;

  PageFormat format;   // The page size, margins, and orientation
  Vector lines;        // The text to be printed, broken into lines
  Font font;           // The font to print with
  int linespacing;     // How much space between lines
  int linesPerPage;    // How many lines fit on a page
  int numPages;        // How many pages required to print all lines
  int baseline = -1;   // The baseline position of the font.

  /** Create a PageableText object for a string of text */
  public PageableText(String text, PageFormat format) throws IOException { 
    this(new StringReader(text), format); 

  /** Create a PageableText object for a file of text */
  public PageableText(File file, PageFormat format) throws IOException { 
    this(new FileReader(file), format); 

  /** Create a PageableText object for a stream of text */
  public PageableText(Reader stream, PageFormat format) throws IOException {
    this.format = format;

    // First, read all the text, breaking it into lines.
    // This code ignores tabs, and does not wrap long lines.
    BufferedReader in = new BufferedReader(stream);
    lines = new Vector();
    String line;
    while((line = in.readLine()) != null) 

    // Create the font we will use, and compute spacing between lines
    linespacing = (int) (FONTSIZE * LINESPACEFACTOR);

    // Figure out how many lines per page, and how many pages
    linesPerPage = (int)Math.floor(format.getImageableHeight()/linespacing);
    numPages = (lines.size()-1)/linesPerPage + 1;

  // These are the methods of the Pageable interface.
  // Note that the getPrintable() method returns this object, which means
  // that this class must also implement the Printable interface.
  public int getNumberOfPages() { return numPages; }
  public PageFormat getPageFormat(int pagenum) { return format; }
  public Printable getPrintable(int pagenum) { return this; }

   * This is the print() method of the Printable interface.
   * It does most of the printing work.
  public int print(Graphics g, PageFormat format, int pagenum) {
    // Tell the PrinterJob if the page number is not a legal one.
    if ((pagenum < 0) | (pagenum >= numPages)) 
      return NO_SUCH_PAGE;

    // First time we're called, figure out the baseline for our font.
    // We couldn't do this earlier because we needed a Graphics object
    if (baseline == -1) {
      FontMetrics fm = g.getFontMetrics(font);
      baseline = fm.getAscent();

    // Clear the background to white.  This shouldn't be necessary, but is
    // required on some systems to workaround an implementation bug
    g.fillRect((int)format.getImageableX(), (int)format.getImageableY(),

    // Set the font and the color we will be drawing with.
    // Note that you cannot assume that black is the default color!

    // Figure out which lines of text we will print on this page
    int startLine = pagenum * linesPerPage;
    int endLine = startLine + linesPerPage - 1;
    if (endLine >= lines.size()) 
      endLine = lines.size()-1;

    // Compute the position on the page of the first line.
    int x0 = (int) format.getImageableX();
    int y0 = (int) format.getImageableY() + baseline;

    // Loop through the lines, drawing them all to the page.
    for(int i=startLine; i <= endLine; i++) {
      // Get the line
      String line = (String)lines.elementAt(i);

      // Draw the line.
      // We use the integer version of drawString(), not the Java 2D
      // version that uses floating-point coordinates. A bug in early
      // Java2 implementations prevents the Java 2D version from working.
      if (line.length() > 0) 
        g.drawString(line, x0, y0);

      // Move down the page for the next line.
      y0 += linespacing;  

    // Tell the PrinterJob that we successfully printed the page.
    return PAGE_EXISTS;

   * This is a test program that demonstrates the use of PageableText
  public static void main(String[] args) throws IOException, PrinterException {
    // Get the PrinterJob object that coordinates everything
    PrinterJob job = PrinterJob.getPrinterJob();

    // Get the default page format, then ask the user to customize it.
    PageFormat format = job.pageDialog(job.defaultPage());

    // Create our PageableText object, and tell the PrinterJob about it
    job.setPageable(new PageableText(new File(args[0]), format));

    // Ask the user to select a printer, etc., and if not canceled, print!
    if (job.printDialog()) 


Related examples in the same category

1.The Printing code which implements Printable
2.Print an Image to print directly
3.Simplest SWT Print ExampleSimplest SWT Print Example
4.Print in Java 2: PrinterJob
5.Print in Java: page format and document
6.Print in Java: Multi page
7.Print in Java 5
8.Print in Java 6
9.Simple Book for printingSimple Book for printing
10.Shapes PrintShapes Print
11.Display the print dialog and print
12.Print the printable area outlinePrint the printable area outline
13.Print the text file and print preview themPrint the text file and print preview them
14.Printable demoPrintable demo
15.Print Swing componentsPrint Swing components
17.Another print demoAnother print demo
18.Book demoBook demo
19.Printing the Combined-Java 1.2-and-1.4 WayPrinting the Combined-Java 1.2-and-1.4 Way
20.Printing the Java 1.4 Way
21.Prompting for a Printer
22.Printing the Java 1.1 WayPrinting the Java 1.1 Way
24.Printable Document
25.PrintFile -- Print a file named on the command linePrintFile -- Print a file named on the command line
26.Print to the standard output
27.PrintPanel is the base for an open-ended series of classesPrintPanel is the base for an open-ended series of classes
28.The area of the printable area
29.The area of the actual page
30.Printing Pages with Different Formats
31.Setting the Orientation of a Printed Page
32.Print Dialog: change the default printer settings(default printer, number of copies, range of pages)
33.Printing to a File
34.Listening for Print Service Status Changes
35.Print Image
36.Overriding the Default Action of a JTextComponent
37.Displaying the Page Format Dialog: changes the default page format such as orientation and paper size.
38.Printable Component
39.Create PageFormats on a higher level
40.Printing of a multi-page bookPrinting of a multi-page book