Printer Problem
I have written some code to print from a file to a printer. The problem is that only the first page prints out and the rest do not. Here's more clarification:
Becasue I assume that in a file about 45 lines make up a normal page I am able to get the right amount of pages to be displayed in the dialog and to print, but still, the first page is printed fine and the rest are blank. I am baffled as to what I'm doing wrong. I have also noticed that many other people have had the same problem, but with no solutions. It seems as though the print method of the printable interface is called twice for every page with a different graphics element, and I don't know if that is what is causing my problem or not. I am using the jdk 1.3.1 - is that a problem? Here is my code and if anyone can propose a solution to the blank pages problem I would be very grateful!
import java.awt.*;
import java.awt.print.*;
import java.io.*;
import javax.swing.*;
public class MyPrinter implements Runnable {
protected PrinterJob job;
protected MyPrintable myPrint = new MyPrintable();
protected MyPageable myPage = new MyPageable();
protected MyDialog dialog = new MyDialog();
protected RandomAccessFile raf;
//protected Startmenu mainMenu;
protected String fileName;
protected File printFile;
protected int numPages = 0;
public MyPrinter() { } // end constructor
public void printFile(String name) {// gives error message if file doesn't exist!
printFile = new File(name);
fileName = name;
if((printFile.exists()) && (printFile.canRead())) {
Thread myThread = new Thread(this);
myThread.start();
} // end if
else {
dialog.showError("Datei "+fileName+" existert nicht!", "Fehler mit Drucken");
} // end else
} // end printFile
public void run() {
numPages = findNumPages();
job = PrinterJob.getPrinterJob();
job.setPageable(myPage); // this is an instance of Pageable
if(job.printDialog()) { // user didn't cancel the printing
try {
job.print();
try { raf.close(); } // end try
catch(IOException e) { dialog.showError("Fehler: "+e.getMessage(), "Problem");}
} // end try
catch (PrinterException pe) {dialog.showError("Fehler: "+pe.getMessage(), "Problem");}
} // end if
} // end run
private int findNumPages() {
try {
numPages = 0;
raf = new RandomAccessFile(fileName, "r");
int fileLength = (int)printFile.length();
int lineNumber = 0;
while((int)raf.getFilePointer() < fileLength) {
lineNumber = 0;
while((((int)raf.getFilePointer()) < fileLength) && (lineNumber < 45)) {
raf.readLine();
lineNumber++;
} // end while loop
numPages++;
} // end while loop
raf.seek(0);
} // end try
catch(IOException e) { dialog.showError("Fehler: "+e.getMessage(), "Problem");}
return numPages;
} // end findNumPages
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
public class MyPageable implements Pageable {
public Printable getPrintable(int pageIndex) { return myPrint; } // end getPrintable
public PageFormat getPageFormat(int pageIndex) {
PageFormat pf = new PageFormat();
pf.setOrientation(PageFormat.PORTRAIT);
return pf;
} // end getPageFormat
public int getNumberOfPages() {
return numPages; // test
//return ((int)printFile.length()/4000); // assume 4KB per page
} // end getNumberOfPages
} // end MyPageable class
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
public class MyPrintable implements Printable {
private Font fnt = new Font("Serif", Font.PLAIN, 11);
protected int lastPageIndexChecked = -1;
public int print(Graphics g, PageFormat pf, int pageIndex)
throws PrinterException {
// pageIndex # corresponds to page number # + 1
pf.setOrientation(PageFormat.PORTRAIT);
if (pageIndex >= numPages) { return Printable.NO_SUCH_PAGE; } // end if
else {
g.setFont(fnt);
g.setColor(Color.black);
String oneLine;
int x = 75, y = 80, lineNumber = 0;
try {
int fileLength = (int)printFile.length();
while((((int)raf.getFilePointer()) < fileLength) && (lineNumber < 45)) {
oneLine = raf.readLine();
lineNumber++;
g.drawString(oneLine, x, y);
y += 15;
} // end while loop
} // end try
catch(IOException e) { dialog.showError("Fehler: "+e.getMessage(), "Problem");}
return Printable.PAGE_EXISTS;
} // end else
} // end print
} // end MyPrintable class
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
} // end MyPrinter class
[4953 byte] By [
abcdefg2] at [2007-9-26 2:36:51]

I was actually able to solve the problem! The print function of the Printable interface is called twice for every page, which was what prevented the pages after the first (the graphics for the other pages) from somehow getting all the info they needed, etc. So here is my updated code for anyone who is having similar problems and needs a similar solution: :)
import java.awt.*;
import java.awt.print.*;
import java.io.*;
import javax.swing.*;
public class MyPrinter implements Runnable {
protected PrinterJob job;
protected MyPrintable myPrint = new MyPrintable();
protected MyPageable myPage = new MyPageable();
protected MyDialog dialog = new MyDialog();
protected RandomAccessFile raf;
protected String fileName;
protected File printFile;
protected int numPages = 0;
protected int[] lastFilePositions;
public MyPrinter() { } // end constructor
public void printFile(String name) {// gives error message if file doesn't exist!
printFile = new File(name);
fileName = name;
if((printFile.exists()) && (printFile.canRead())) {
Thread myThread = new Thread(this);
myThread.start();
} // end if
else {
dialog.showError("Datei "+fileName+" existert nicht!", "Fehler mit Drucken");
} // end else
} // end printFile
public void run() {
numPages = findNumPages();
job = PrinterJob.getPrinterJob();
job.setPageable(myPage); // this is an instance of Pageable
if(job.printDialog()) { // user didn't cancel the printing
try {
job.print();
try { raf.close(); } // end try
catch(IOException e) { dialog.showError("Fehler: "+e.getMessage(), "Problem");
System.exit(0);
} // end catch
} // end try
catch (PrinterException pe) {dialog.showError("Fehler: "+pe.getMessage(), "Problem");
System.exit(0);
} // end catch
} // end if
} // end run
private int findNumPages() {
try {
numPages = 0;
raf = new RandomAccessFile(fileName, "r");
int fileLength = (int)printFile.length();
int lineNumber = 0;
while((int)raf.getFilePointer() < fileLength) {
lineNumber = 0;
while((((int)raf.getFilePointer()) < fileLength) && (lineNumber < 45)) {
raf.readLine();
lineNumber++;
} // end while loop
numPages++;
} // end while loop
raf.seek(0);
lastFilePositions = new int[numPages];
intializeLastFilePositions();
} // end try
catch(IOException e) { dialog.showError("Fehler: "+e.getMessage(), "Problem");
System.exit(0);
} // end catch
return numPages;
} // end findNumPages
private void intializeLastFilePositions() {
for(int i = 0; i < lastFilePositions.length; i++) {
lastFilePositions = 0;
} // end for loop
} // end intializeLastFilePositions
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
public class MyPageable implements Pageable {
public Printable getPrintable(int pageIndex) { return myPrint; } // end getPrintable
public PageFormat getPageFormat(int pageIndex) {
PageFormat pf = new PageFormat();
pf.setOrientation(PageFormat.PORTRAIT);
return pf;
} // end getPageFormat
public int getNumberOfPages() {
return numPages; // test
//return ((int)printFile.length()/4000); // assume 4KB per page
} // end getNumberOfPages
} // end MyPageable class
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
public class MyPrintable implements Printable {
private Font fnt = new Font("Serif", Font.PLAIN, 11);
protected int startAtLineNumber = 0;
public int print(Graphics g, PageFormat pf, int pageIndex)
throws PrinterException {
// pageIndex # corresponds to page number # + 1
pf.setOrientation(PageFormat.PORTRAIT);
if(pageIndex > numPages) return NO_SUCH_PAGE;
else {
if(pageIndex == 0) { startAtLineNumber = 0; } // end if
else {
startAtLineNumber = lastFilePositions[pageIndex-1];
} // end else
g.setFont(fnt);
g.setColor(Color.black);
String oneLine;
int x = 75, y = 80, lineNum = 0;
try {
raf.seek(startAtLineNumber);
int fileLength = (int)printFile.length();
while((((int)raf.getFilePointer()) < fileLength) && (lineNum < 45)) {
oneLine = raf.readLine();
if(oneLine.length() > 130) { // wrap lines if one line is too long
String secondLine = oneLine.substring(130, oneLine.length());
oneLine = oneLine.substring(0, 130);
g.drawString(oneLine, x, y);
y += 15;
g.drawString(secondLine, x, y);
y += 15;
} // end if
else { // line fit on one line
g.drawString(oneLine, x, y);
y += 15;
} // end else
lineNum++;
} // end while loop
lastFilePositions[pageIndex] = (int)raf.getFilePointer();
} // end try
catch(IOException e) { dialog.showError("Fehler: "+e.getMessage(), "Problem");
System.exit(0);
} // end catch
return Printable.PAGE_EXISTS;
} // end else
} // end print
} // end MyPrintable class
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
} // end MyPrinter class