Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

To print a document  RSS feed

 
Jude Fawly
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.
 
clio katz
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
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!