N
Neil via JavaKB.com
I'm trying to print multiple pages of text from a Java app, using the Book
class, but even though all the pages are sent to the printer the first page
is the only one that actually prints...all the following pages are blank.
Does anyone have an idea why this might be? Here's the code:
//vPage is a String vector containing lines of text
public void PrintIt()
{
PrinterJob printJob = PrinterJob.getPrinterJob ();
Book book = new Book ();
PageFormat documentPageFormat = new PageFormat ();
documentPageFormat.setOrientation (PageFormat.PORTRAIT);
for (int i=0, j=1; i < vPage.size(); i += 52, j++ )
{
NumberOfPages());
book.append (new Document (), documentPageFormat);
}
printJob.setPageable (book);
if (printJob.printDialog()) {
try {
printJob.print();
} catch (Exception PrintException) {
PrintException.printStackTrace();
}
}
}
private class Document implements Printable
{
public int print (Graphics g, PageFormat pageFormat, int page) {
Font fnt = new Font("Arial", Font.PLAIN, 10);
Graphics2D g2d = (Graphics2D) g;
g2d.translate (pageFormat.getImageableX (),
pageFormat.getImageableY ());
g2d.setPaint (Color.black);
g2d.setFont(fnt);
FontMetrics fontMetrics = g2d.getFontMetrics ();
System.out.println("pageIndex=" + page);
Paper paper = new Paper();
paper.setImageableArea(7.5,10,8.5,11);
pageFormat.setPaper(paper);
String text = "";
double j= 70;
int limit;
if (vPage.size() < 51)
limit = vPage.size() - 1;
else
limit = 52;
while (linesPrinted < (vPage.size() - 1))
{
for (int i = 0;i < limit; i++)
{
text = (String) vPage.get(i);
LineMetrics lm =
fnt.getLineMetrics(text,g2d.getFontRenderContext())
g2d.drawString (text, 75, (int) j);
j=j + (double) lm.getHeight();
linesPrinted++;
if (linesPrinted==(vPage.size()-1))
break;
}
System.out.println("returning PAGE_EXISTS");
return (Printable.PAGE_EXISTS);
}
return (Printable.PAGE_EXISTS);
}
class, but even though all the pages are sent to the printer the first page
is the only one that actually prints...all the following pages are blank.
Does anyone have an idea why this might be? Here's the code:
//vPage is a String vector containing lines of text
public void PrintIt()
{
PrinterJob printJob = PrinterJob.getPrinterJob ();
Book book = new Book ();
PageFormat documentPageFormat = new PageFormat ();
documentPageFormat.setOrientation (PageFormat.PORTRAIT);
for (int i=0, j=1; i < vPage.size(); i += 52, j++ )
{
NumberOfPages());
book.append (new Document (), documentPageFormat);
}
printJob.setPageable (book);
if (printJob.printDialog()) {
try {
printJob.print();
} catch (Exception PrintException) {
PrintException.printStackTrace();
}
}
}
private class Document implements Printable
{
public int print (Graphics g, PageFormat pageFormat, int page) {
Font fnt = new Font("Arial", Font.PLAIN, 10);
Graphics2D g2d = (Graphics2D) g;
g2d.translate (pageFormat.getImageableX (),
pageFormat.getImageableY ());
g2d.setPaint (Color.black);
g2d.setFont(fnt);
FontMetrics fontMetrics = g2d.getFontMetrics ();
System.out.println("pageIndex=" + page);
Paper paper = new Paper();
paper.setImageableArea(7.5,10,8.5,11);
pageFormat.setPaper(paper);
String text = "";
double j= 70;
int limit;
if (vPage.size() < 51)
limit = vPage.size() - 1;
else
limit = 52;
while (linesPrinted < (vPage.size() - 1))
{
for (int i = 0;i < limit; i++)
{
text = (String) vPage.get(i);
LineMetrics lm =
fnt.getLineMetrics(text,g2d.getFontRenderContext())
g2d.drawString (text, 75, (int) j);
j=j + (double) lm.getHeight();
linesPrinted++;
if (linesPrinted==(vPage.size()-1))
break;
}
System.out.println("returning PAGE_EXISTS");
return (Printable.PAGE_EXISTS);
}
return (Printable.PAGE_EXISTS);
}