• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Print a JtextArea

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm too new to Java to know how to print athe content of a JTextArea, so I hope you'll help me. What I would like to do is printing what is inside a JTextArea exactly as if it was a Wor document. That means more than a page, lines printed correctly and so on. I don't need to print images. I'm actually using this code:
void Stampare(){
PrinterJob printJob = PrinterJob.getPrinterJob();
Book book = new Book();
PageFormat documentPageFormat = new PageFormat();
//documentPageFormat.setOrientation(PageFormat.LANDSCAPE);
documentPageFormat.setOrientation(PageFormat.PORTRAIT);
book.append(new Document(), documentPageFormat);
printJob.setPageable(book);
if (printJob.printDialog()) {
try {
printJob.print();
}
catch (Exception PrintException){
PrintException.printStackTrace();
}
}
}

public class Document implements Printable {
private final static int POINTS_PER_INCH = 72;
public int print (Graphics g, PageFormat pageFormat, int page) {
Graphics2D g2d = (Graphics2D) g;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
g2d.setPaint(Color.black);
g2d.setStroke(new BasicStroke (4));
Rectangle2D.Double border = new Rectangle2D.Double (0, 0, pageFormat.getImageableWidth (), pageFormat.getImageableHeight ());
//g2d.draw(border);
//String text = new String ("Kalle");
String text = new String(areaStampa.getText());
Point2D.Double pen = new Point2D.Double(0.25 * POINTS_PER_INCH, 0.25 * POINTS_PER_INCH);
double width = 7.5 * POINTS_PER_INCH;
AttributedString paragraphText = new AttributedString(text);
paragraphText.addAttribute(TextAttribute.FONT, new Font("serif", Font.PLAIN, 12));
LineBreakMeasurer lineBreaker = new LineBreakMeasurer(paragraphText.getIterator(), new FontRenderContext(null, true, true));
TextLayout layout;
while ((layout = lineBreaker.nextLayout((float) width)) != null){
pen.y += layout.getAscent();
layout.draw(g2d, (float) pen.x, (float) pen.y);
pen.y += layout.getDescent() + layout.getLeading();
}
return (PAGE_EXISTS);
}
}
This code doesn't work properly. First of all I get errors about security things that I don't even understand. Secondly, it prints all the lines one after the other, without carriage returns (hope you understood this! ). Moreover, some letters are not printed, as if they were outside the printing area.
Can you please help me finding a solution?
Thanks a lot,
Giorgio
 
giorginobello
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still need a lot of help with this.
Thanks,
Giorgio
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have found a useful document on the Sun site see URL below it may help you out.
http://java.sun.com/products/java-media/2D/forDevelopers/sdk12print.html
Ian
reply
    Bookmark Topic Watch Topic
  • New Topic