The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

How to print string text in a frame with line wraps

Posted by Karen Kwon on November 30, 2001 at 10:41 PM

Hello,
I am trying really hard to print a textarea in a frame. It prints string to graphics via printjob but it does not deal with word wrap so that when I print, I lose all the info that doesn't fit in the paper (to the right of the paper).

Here's my code implementing the command:

public void printCommand() {
PrintJob pjob = getToolkit().getPrintJob(FileViewer.this, "FilePrinter", printprefs);
if (pjob != null)
{
Graphics pg = pjob.getGraphics();
if (pg != null)
{
pg.translate(75, 75);
Dimension size = this.getSize();
// Set a clipping region so our scribbles don't go outside the border
// On-screen this happens automatically, but not on paper.
//pg.setClip(0, 0, size.width, size.height);
String s = textarea.getText();
printLongString(pjob, pg, s);
pg.dispose();
}
pjob.end();
}
}


// Print string to graphics via printjob
// Does not deal with word wrap or tabs
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;
Font myfont = new Font("Tahoma", Font.PLAIN, 9);
//have to set the font to get any output
pg.setFont (myfont);
FontMetrics fm = pg.getFontMetrics(myfont);
int fontHeight = fm.getHeight();
int fontDescent = fm.getDescent();
int curHeight = 0;
try {
do {
nextLine = lnr.readLine();
if (nextLine != null) {
if ((curHeight + fontHeight) > pageHeight) {
// New Page
System.out.println ("" + linesForThisPage + " lines printed for page " + pageNum);
pageNum++;
linesForThisPage = 0;
pg.dispose();
pg = pjob.getGraphics();
if (pg != null) {
pg.setFont (myfont);
}
curHeight = 0;
}
curHeight += fontHeight;
if (pg != null) {
pg.drawString (nextLine, 0, 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);
}

Can anyboy help me to implemenet wordwrap when printing?
Any help will be greatly appreciated and thank you so much in advance....

Thanks!!!



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us