aspose file tools*
The moose likes I/O and Streams and the fly likes Hang on printing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Hang on printing" Watch "Hang on printing" New topic
Author

Hang on printing

michael yue
Ranch Hand

Joined: Nov 20, 2003
Posts: 204
Hi all,

I am facing a problem with printing. I am trying to straight away print something without generating the file first. Problem is the data to print can be very long horizontally. When the print dialog box pop up and i click ok, it just hangs there. when i delete some of the data to print, it appears fine. Is there anything i can do to prevent this printing hang. My sample code is below. Thanks for the help

// Print a file into the text area.
class PrintCommand implements ActionListener {
public void actionPerformed (ActionEvent e) {
PrintJob pjob = getToolkit().getPrintJob(FrmPrint.this, "printRpt", p);
if (pjob != null) {
Graphics pg = pjob.getGraphics();
if (pg != null) {
String s = textArea.getText();
printLongString (pjob, pg, s);
pg.dispose();
}
pjob.end();
}
}
}

// I'm assuming a one-inch margin on all
// four sides. This could be done better.
private int margin = 72;

// Print string to graphics via printjob
// Does not deal with word wrap or tabs
private void printLongString (PrintJob pjob, Graphics pg, String s) {

int pageNum = 1;
int linesForThisPage = 0;
int linesForThisJob = 0;
// Note: String is immutable so won't change while printing.
if (!(pg instanceof PrintGraphics)) {
throw new IllegalArgumentException ("Graphics context not PrintGraphics");
}
StringReader sr = new StringReader (s);
LineNumberReader lnr = new LineNumberReader (sr);
String nextLine;
int pageHeight = pjob.getPageDimension().height - margin;
Font helv = new Font("Courier", Font.PLAIN, 10);//Helvetica
//have to set the font to get any output
pg.setFont (helv);
FontMetrics fm = pg.getFontMetrics(helv);
int fontHeight = fm.getHeight();
int fontDescent = fm.getDescent();
int curHeight = margin;
try {
do {
nextLine = lnr.readLine();
if (nextLine != null) {
if ((curHeight + fontHeight) > pageHeight) {
// New Page
System.out.println ("" + linesForThisPage + " lines printed for page " + pageNum);
if (linesForThisPage == 0) {
System.out.println ("Font is too big for pages of this size; aborting...");
break;
}
pageNum++;
linesForThisPage = 0;
pg.dispose();
pg = pjob.getGraphics();
if (pg != null) {
pg.setFont (helv);
}
curHeight = 0;
}
curHeight += fontHeight;
if (pg != null) {
pg.drawString (nextLine, margin, curHeight - fontDescent);
linesForThisPage++;

linesForThisJob++;
} else {
System.out.println ("pg null");
}
}
} while (nextLine != null);
} catch (EOFException eof) {
// Fine, ignore
} catch (Throwable t) { // Anything else
t.printStackTrace();
}
System.out.println ("" + linesForThisPage + " lines printed for page " + pageNum);
System.out.println ("pages printed: " + pageNum);
System.out.println ("total lines printed: " + linesForThisJob);
}
 
 
subject: Hang on printing