0

I need to print line by line in a thermal printer. line breaking at %n I would like to persist the string pattern, while printing.. I'm not well versed with printer api and graphic 2d api.. I need to fix this 1 hour time.. Would appreciate a quick answer.. Thanks in advance My String format is like this:

String printStat = 
                        "              *****                  %n"
                      + "       W*** OF ** AND *****        %n"
                      + "      4/400 kfjkasjfdkas ajdksa        %n"
                      + "  aksdka ajke ajeklaje  kajke ka  a   %n"
                      + " Date: "+now.get(Calendar.DAY_OF_MONTH)+"/"+(now.get(Calendar.MONTH)+1)+"/"+now.get(Calendar.YEAR)+"             Time: "+now.get(Calendar.HOUR_OF_DAY)+":"+now.get(Calendar.MINUTE)+"%n"
                      + "--------------------------------------%n"
                      + "    Name            Qty     Price    %n"
                      + "--------------------------------------%n";

print method:

@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
       /* We'll assume that Jav2D is available. Create a copy
        * of it so that we can pass the original Graphics
        * instance to the PageFormat instance.
        */
        Graphics2D g2d = (Graphics2D) graphics.create();
        /* Move the origin from the corner of the Paper to the corner
        * of the imageable area.
        */
        g2d.translate(format.getImageableX(), format.getImageableY());
        /* Set the text color.
        */
        g2d.setPaint(Color.black);
        g2d.setFont(new Font("Arial", Font.BOLD, 10));
        /* Use a LineBreakMeasurer instance to break our text into
        * lines that fit the imageable area of the page.
        */
        Point2D.Float pen = new Point2D.Float();
        AttributedCharacterIterator charIterator = mStyledText.getIterator();
        //LineBreakMeasurer measurer = new LineBreakMeasurer(charIterator, g2d.getFontRenderContext());
        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();
        }
        g2d.dispose();
        g2d = null;
        /* Calling the PageFormat is not part of the printing API, 
        * but it is a useful convention. In this example PageFormat 
        * does not implement Printable and so it is not invoked here. 
        * In later examples, PageFormat will implement Printable. 
        */
        try {
        Printable formatPainter = (Printable) format;
        formatPainter.print(graphics, format, pageIndex);
        /* Nothing to do here. The PageFormat has nothing to print.
        */
        } catch (ClassCastException exception) {
        }
        return Printable.PAGE_EXISTS;
       }   
bmusical
  • 244
  • 3
  • 14

1 Answers1

1

Use a fixed width font, replace Arial with "Monospaced" or "Courier".

Emmanuel Bourg
  • 9,601
  • 3
  • 48
  • 76
  • Thanks that really helped to have an uniform width. For line break, i have modified the print method iterating the array made by splitting the print string at delimiters(End of line) and drawing each line. Not sure whether this is the best way.. but worked!! – bmusical May 30 '12 at 09:26