English 中文(简体)
如何使用打印机 Api 在 JAVA 中格式化打印文本?
原标题:How to format text for printing in JAVA using printer api?

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";

打印方法 :

@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;
       }   
最佳回答

使用固定宽度字体, 将 Arial 替换为“ 月球” 或“ 库里尔 ” 。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签