加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

如何在java中打印带换行符的字符串

发布时间:2020-12-14 05:33:05 所属栏目:Java 来源:网络整理
导读:我需要使用 java打印一个字符串,所以我喜欢以下解决方案经过google搜索了很多.我做了一些更改来打印字符串而不显示打印对话框.我的问题是虽然这个方法正确地打印了字符串,但它不会像我定义的那样破坏行.请告诉我如何打印带换行符的字符串. public class Prin
我需要使用 java打印一个字符串,所以我喜欢以下解决方案经过google搜索了很多.我做了一些更改来打印字符串而不显示打印对话框.我的问题是虽然这个方法正确地打印了字符串,但它不会像我定义的那样破坏行.请告诉我如何打印带换行符的字符串.
public class PrintBill implements Printable {

    private static final String mText = "SHOP MAn"
            + "----------------------------n"
            + "Pannampitiyan"
            + "09-10-2012 harsha  no: 001n"
            + "No  Item  Qty  Price  Amountn"
            + "1 Bread 1 50.00  50.00n"
            + "____________________________n";

    private static final AttributedString mStyledText = new AttributedString(mText);

    static public void main(String args[]) throws PrinterException {
        PrinterService ps = new PrinterService();
        PrintService pss = ps.getCheckPrintService("Samsung-ML-2850D-2");//get the printer service by printer name


        PrinterJob printerJob = PrinterJob.getPrinterJob();
        printerJob.setPrintService(pss);

        Book book = new Book();
        book.append(new PrintBill(),new PageFormat());       

        printerJob.setPageable(book);


        try {
            printerJob.print();
            System.out.println(printerJob.getPrintService().getName());
            System.out.println("Print compleated..");
        } catch (PrinterException exception) {
            System.err.println("Printing error: " + exception);
            exception.printStackTrace();
        }

    @Override
    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;
    }
}

打印机服务提供课程

public class PrinterService {

    public PrintService getCheckPrintService(String printerName) {
        PrintService ps = null;
        DocFlavor doc_flavor = DocFlavor.STRING.TEXT_PLAIN;
        PrintRequestAttributeSet attr_set =
                new HashPrintRequestAttributeSet();

        attr_set.add(new Copies(1));
        attr_set.add(Sides.ONE_SIDED);
        PrintService[] service = PrintServiceLookup.lookupPrintServices(doc_flavor,attr_set);

        for (int i = 0; i < service.length; i++) {
            System.out.println(service[i].getName());
            if (service[i].getName().equals(printerName)) {
                ps = service[i];
            }
        }
        return ps;
    }
}

解决方法

这样做: –
String newline = System.getProperty("line.separator");

private static final String mText = "SHOP MA" + newline +
        + "----------------------------" + newline +
        + "Pannampitiya" + newline +
        + "09-10-2012 harsha  no: 001" + newline +
        + "No  Item  Qty  Price  Amount" + newline +
        + "1 Bread 1 50.00  50.00" + newline +
        + "____________________________" + newline;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读