Name: rv122619 Date: 05/07/2004
Using jdk1.5.0 (build 49)
Run the following program, which prints a line of text using two different strategies. The first prints the default way, passing no arguments to printDialog() and print(). The second way passes a PrintRequestAttributeSet to both functions. In jdk1.4.2 (b28), the output from both versions is the same. However, in 1.5.0 (with a particular printer, see below), the output of the first print attempt appears to be compressed horizontally. The same number of characters is printed by each strategy; however, the characters printed using the 1st strategy are narrower. The line of text is truncated about 2/3 of the way across the page instead of at the default 1 inch margin. In a previous test, I confirmed that filling the background of the imageable rectangle only fills up 2/3 of the page across (but seems okay vertically).
Printing to a file (and viewing with Ghostscript) shows the same behavior.
On two printers HP LaserJet 5Si/5Si MX PS and HP LaserJet IIISi Postscript v52.3, the output is the same using either strategy. However, the printer where I see the difference is a Tektronix Phaser 850 DP (color and higher resolution I think).
import java.awt.Graphics;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
public class PrintTest implements Printable
{
/**
*
*/
public PrintTest()
{
super();
}
public static void main(String[] args)
{
PrintTest pt=new PrintTest();
pt.printFails();
pt.printWorks();
}
public void printFails()
{
PrinterJob job=PrinterJob.getPrinterJob();
job.setPrintable(this);
if (job.printDialog())
{
try
{
job.print();
}
catch (PrinterException e)
{
e.printStackTrace();
}
}
}
public void printWorks()
{
PrinterJob job=PrinterJob.getPrinterJob();
job.setPrintable(this);
PrintRequestAttributeSet settings=new HashPrintRequestAttributeSet();
if (job.printDialog(settings))
{
try
{
job.print();
}
catch (PrinterException e)
{
e.printStackTrace();
}
}
}
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException
{
if (pageIndex>0)
{
return NO_SUCH_PAGE;
}
StringBuffer s=new StringBuffer();
for (int i=0;i<10;i++)
{
s.append("1234567890ABCDEFGHIJ");
}
int x=(int) pageFormat.getImageableX();
int y=(int) (pageFormat.getImageableY()+50);
graphics.drawString(s.toString(), x, y);
return PAGE_EXISTS;
}
}
======================================================================