Java Reference
In-Depth Information
that is wider than the requested page width (in an HTML <PRE> tag, for example),
the Document class makes the entire document as wide as the widest line. Thus, it
is not just wide lines within <PRE> tags that get truncated; the entire document is
formatted to be too wide, causing all lines to get truncated at the right margin of
the printed page.
Example 12−4: PrintableDocument.java
package com.davidflanagan.examples.print;
import java.awt.*;
import java.awt.print.*;
import java.awt.geom.*;
import java.awt.font.*;
import javax.swing.*;
import javax.swing.text.*;
import java.util.*;
/**
* This class implements the Pageable and Printable interfaces and allows
* the contents of any JTextComponent to be printed using the java.awt.print
* printing API.
**/
public class PrintableDocument implements Pageable, Printable {
View root; // The root View to be printed
PageFormat format; // Paper plus page orientation
double scalefactor; // How much to scale before printing
int numPages; // How many pages in the document
double printX, printY; // coordinates of upper-left of print area
double printWidth;
// Width of the printable area
double printHeight;
// Height of the printable area
Rectangle drawRect;
// The rectangle in which the document is painted
// How lenient are we with the bottom margin in widow/orphan prevention?
static final double MARGIN_ADJUST = .97;
// The font we use for printing page numbers
static final Font headerFont = new Font("Serif", Font.PLAIN, 12);
/**
* This constructor allows printing the contents of any JTextComponent
* using a default PageFormat and a default scale factor. The default
* scale factor is .75 because the default fonts are overly large.
*/
public PrintableDocument(JTextComponent textComponent) {
this(textComponent, new PageFormat(), .75);
}
/**
* This constructor allows the contents of any JTextComponent to be
* printed, using any specified PageFormat object and any scaling factor.
**/
public PrintableDocument(JTextComponent textComponent, PageFormat format,
double scalefactor)
{
// Remember the page format, and ask it for the printable area
this.format = format;
this.scalefactor = scalefactor;
this.printX = format.getImageableX()/scalefactor;
this.printY = format.getImageableY()/scalefactor;
this.printWidth = format.getImageableWidth()/scalefactor;
Search WWH ::




Custom Search