• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

To print a document

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
We don't have time for this. We've gotta save the moon! Or check this out:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic