This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

To print a document  RSS feed

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

Can somebody help me to make my text to be printer by the printer in a nice pattern. Now I am using this code:
-------------------------------------------------------------------------------------------
class B{
PrinterJob printerJob = PrinterJob.getPrinterJob();
Book book = new Book();
book.append(new PrintReceipt(), new PageFormat());
printerJob.setPageable(book);
boolean doPrint = printerJob.printDialog();
if (doPrint)
{
try
{
printerJob.print();
}
catch (PrinterException exception)
{
System.err.println("Printing error: " + exception);
}
}

public class PrintReceipt implements Printable
{
private static String TEXT = "my name: \n\txx\n\tmy age:xx";
private static AttributedString mStyledText= new AttributedString(TEXT);


public int print(Graphics g, PageFormat format, int pageIndex)
{
Graphics2D g2d = (Graphics2D) g;
g2d.translate(format.getImageableX(), format.getImageableY());
g2d.setPaint(Color.black);
Point2D.Float pen = new Point2D.Float();
AttributedCharacterIterator charIterator = mStyledText.getIterator();
LineBreakMeasurer measurer = new LineBreakMeasurer(charIterator, g2d.getFontRenderContext());
float wrappingWidth = (float) format.getImageableWidth();

while (measurer.getPosition() < charIterator.getEndIndex())
{
TextLayout layout = measurer.nextLayout(wrappingWidth);
pen.y += layout.getAscent();
float dx = layout.isLeftToRight()? 0 : (wrappingWidth - layout.getAdvance());
layout.draw(g2d, pen.x + dx, pen.y);
pen.y += layout.getDescent() + layout.getLeading();
}

return Printable.PAGE_EXISTS;
}
}
---------------------------------------------------------------------------------------
Even by using carriage return and tab(\n, \t) they have no effect on the printed text; they appear on the printer document as one word next to the other.
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm not a print expert, but i *have* printed formatted pages using HTML doc kit. actually, i did some research and i came to a standstill with all other methods (unless you want to use pure graphics).

essentially, you have to consider that java printing just renders what you 'paint' onto a graphics device (e.g. printer) using the graphics canvas you give it, and the attributes that you assign the device [o, but don't try too hard to force any device characteristics... java is not very smart about detecting the true device capabilities...]

so if you choose to try this, the first step would be to get your text html-formatted [several jcomponent classes are capable]. you will have to find or write code to translate special chars like newline(<br> , and use java's fuddy old version of html...

perhaps that is what you meant to achieve with attributedstring? i think this is just for specially annotated text ...

hth
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!