Ah mein alter Thread
Da war damals ein Murks.
Eventuell hilft das:
Code:
import java.*;
import java.awt.*;
import java.awt.GraphicsEnvironment;
import java.io.*;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import javax.print.attribute.*;
import javax.print.event.*;
import javax.print.event.PrintJobListener;
import javax.print.event.PrintJobEvent;
public class Ergebnis {
// Drucken des Ergebnisses
static void drucken (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;
int pageWidth = pjob.getPageDimension().width;
Font helv = new Font("Helvetica", Font.PLAIN, 9);
//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 = 40;
try {
do {
nextLine = lnr.readLine();
if (nextLine != null) {
if ((curHeight + fontHeight) > (pageHeight-30)) {
// New Page
Eventlog.add ("" + linesForThisPage + " lines printed for page " + pageNum +"\n");
pageNum++;
linesForThisPage = 0;
pg.dispose();
pg = pjob.getGraphics();
if (pg != null) {
pg.setFont (helv);
}
curHeight = 40;
}
curHeight += fontHeight;
if (pg != null) {
String prefix="";
while (fm.stringWidth(nextLine)>(pageWidth-100)) {
while (nextLine.indexOf("\t")!=-1) {
nextLine=nextLine.replaceFirst("\t"," ");
prefix+=" ";
}
String templine=nextLine;
while(fm.stringWidth(templine)>(pageWidth-100)) {
if (templine.lastIndexOf(' ')!=-1) {
templine=templine.substring(0,templine.lastIndexOf(' '));
} else {
templine=templine.substring(0,templine.length()-1);
}
}
pg.drawString (templine, 40, curHeight - fontDescent);
nextLine=prefix + " " + nextLine.substring(templine.length());
linesForThisPage++;
linesForThisJob++;
curHeight += fontHeight;
}
pg.drawString (nextLine.replaceAll("\t"," "), 40, curHeight - fontDescent);
linesForThisPage++;
linesForThisJob++;
} else {
Eventlog.add ("pg null" +"\n");
}
}
} while (nextLine != null);
}
catch (EOFException eof) {Eventlog.add(eof.toString() +"\n");}
catch (Throwable t) {Eventlog.add(t.toString() +"\n");}
Eventlog.add ("" + linesForThisPage + " lines printed for page " + pageNum +"\n");
Eventlog.add ("pages printed: " + pageNum +"\n");
Eventlog.add ("total lines printed: " + linesForThisJob +"\n");
}
}