Hallo,
habe das Problem gelöst. Vielen Dank an Smash & tele für die super Hilfe.
Geholfen hat mir folgendes Tutorial:
LINK
Wer es nicht durcharbeiten will, der kann es ja mit meiner Lösung versuchen. Diese ist aus diesem Tutorial (Listing 4), wobei ich den Code an mein Programm angepasst habe.
--------------------------------------------------------------------------------
Lösung
Um einen Text zu drucken, könnt ihr folgendes tun:
1. Implementiert die Klasse "Print" in euer Projekt:
Code:
/**
* @author Jean-Pierre Dube <jpdube@videotron.ca>
* @version 1.0
* @since 1.0
*/
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.print.*;
import java.text.*;
public class Print {
//--- Private instances declarations
private final static int POINTS_PER_INCH = 72;
private static String text = new String();
/**
* Constructor
*
*/
public Print(String content) {
//--- String content includes the Text, which should be printed
text = content;
//--- Create a new PrinterJob object
PrinterJob printJob = PrinterJob.getPrinterJob ();
//--- Create a new book to add pages to
Book book = new Book ();
//--- Add the document page using a landscape page format
PageFormat documentPageFormat = new PageFormat ();
documentPageFormat.setOrientation (PageFormat.PORTRAIT);
book.append (new Document (), documentPageFormat);
//--- Tell the printJob to use the book as the pageable object
printJob.setPageable (book);
//--- Show the print dialog box. If the user click the
//--- print button we then proceed to print else we cancel
//--- the process.
if (printJob.printDialog()) {
try {
printJob.print();
} catch (Exception PrintException) {
PrintException.printStackTrace();
}
}
}
private class Document implements Printable {
/**
* Method: print <p>
*
* @param g a value of type Graphics
* @param pageFormat a value of type PageFormat
* @param page a value of type int
* @return a value of type int
*/
public int print (Graphics g, PageFormat pageFormat, int page) {
//--- Create the Graphics2D object
Graphics2D g2d = (Graphics2D) g;
//--- Translate the origin to 0,0 for the top left corner
g2d.translate (pageFormat.getImageableX (), pageFormat.getImageableY ());
//--- Set the drawing color to black
g2d.setPaint (Color.black);
//--- Create a point object to set the top left corner of the TextLayout object
Point2D.Double pen = new Point2D.Double (0.25 * POINTS_PER_INCH, 0.25 * POINTS_PER_INCH);
//--- Set the width of the TextLayout box
double width = 6.0 * POINTS_PER_INCH;
//--- Create an attributed string from the text string. We are creating an
//--- attributed string because the LineBreakMeasurer needs an Iterator as
//--- parameter.
AttributedString paragraphText = new AttributedString (text);
//--- Set the font for this text
paragraphText.addAttribute (TextAttribute.FONT, new Font ("serif", Font.PLAIN, 12));
//--- Create a LineBreakMeasurer to wrap the text for the TextLayout object
//--- Note the second parameter, the FontRendereContext. I have set the second
//--- parameter antiAlised to true and the third parameter useFractionalMetrics
//--- to true to get the best possible output
LineBreakMeasurer lineBreaker = new LineBreakMeasurer (paragraphText.getIterator(), new FontRenderContext (null, true, true));
//--- Create the TextLayout object
TextLayout layout;
//--- LineBreakMeasurer will wrap each line to correct length and
//--- return it as a TextLayout object
while ((layout = lineBreaker.nextLayout ((float) width)) != null) {
//--- Align the Y pen to the ascend of the font, remember that
//--- the ascend is origin (0, 0) of a font. Refer to figure 1
pen.y += layout.getAscent ();
//--- Draw the line of text
layout.draw (g2d, (float) pen.x, (float) pen.y);
//--- Move the pen to the next position adding the descent and
//--- the leading of the font
pen.y += layout.getDescent () + layout.getLeading ();
}
//--- Validate the page
return (PAGE_EXISTS);
}
}
} // Print
2. Nun noch die aufrufende Methode in euer Programm einbinden
Meine sieht so aus:
Code:
public void printJob() {
// content wird der Text der JTextArea "taContent" zugewiesen
String content = taContent.getText();
//Nun wird dieser String an die Klasse "Print" übergeben, die diesen ausdruckt
Print printdoc = new Print(content);
}
So, dass wars. Viel Spaß beim drucken.