printing JTextArea content with swing
843804Jan 23 2002 — edited Aug 11 2006hello,
I would like to know how to print the content of a JTextArea (or JEditorPane) with swing. I followed the tutorial you can find on the sun website. But it only gives information with the fiunctions:
(paintCover class): drawString() which only works with only one line
(paintContent class): drawShapes() for images...
I would like to make it for a whole text (JTextArea) or a text formatted in HTML(JEditorPane)
to give some clues(see the bottom lines which come in the tutorial).
I will be extremely grateful if someone has an idea to do those printings.
thank you very much
bye
// In the program's job control code...
// Get a PrinterJob
PrinterJob job = PrinterJob.getPrinterJob();
// Create a landscape page format
PageFormat landscape = job.defaultPage();
landscape.setOrientation(PageFormat.LANDSCAPE);
// Set up a book
Book bk = new Book();
bk.append(new PaintCover(), job.defaultPage());
bk.append(new PaintContent(), landscape);
// Pass the book to the PrinterJob
job.setPageable(bk);
class PaintCover implements Printable {
Font fnt = new Font("Helvetica-Bold", Font.PLAIN, 48);
public int print(Graphics g, PageFormat pf, int pageIndex)
throws PrinterException {
g.setFont(fnt);
g.setColor(Color.black);
g.drawString("Sample Shapes", 100, 200);
return Printable.PAGE_EXISTS;
}
}
class PaintContent implements Printable {
public int print(Graphics g, PageFormat pf, int pageIndex)
throws PrinterException {
SimpleBook.drawShapes((Graphics2D) g);
return Printable.PAGE_EXISTS;
}