Java编程打印购物小票实现代码

这篇文章主要介绍了Java编程打印购物小票实现代码,具有一定参考价值,需要的朋友可以了解下。

简单介绍运行环境:

语言:Java

工具:eclipse

系统:Windows7

(打印设备暂时没有,所以只能提供预览图)

最近,项目需要为商城做一个购物小票的打印功能,日常我们去超市买东西,结账的时候收银员都会打印一个小票,一般的商城也都需要这样的一个小功能,本文给出的 demo 是在 58mm 的热敏打印机下的例子,如果是其他纸张类型的打印机,调整纸张宽度即可。

package test; import java.awt.*; import java.awt.print.*; /** * 打印机测试类(58mm) * 1、目标打印机必须设置为认打印机 * 2、打印页面的宽度和具体的打印机有关,一般为打印纸的宽度,需要配置成系统参数 * 3、一个汉字的宽度大概是12点 */ public class PrintTest { public static void main(String[] args){ if(PrinterJob.lookupprintServices().length>0){ /* 打印格式 */ PageFormat pageFormat = new PageFormat(); //设置打印起点从左上角开始,从左到右,从上到下打印 pageFormat.setorientation(PageFormat.PORTRAIT); /* 打印页面格式设置 */ Paper paper = new Paper(); //设置打印宽度(固定,和具体的打印机有关)和高度(跟实际打印内容的多少有关) paper.setSize(140, 450); //设置打印区域 打印起点坐标、打印的宽度和高度 paper.setimageableArea(0, 0, 135, 450); pageFormat.setPaper(paper); //创建打印文档 Book book = new Book(); book.append(new Printable() { @Override public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { if(pageIndex>0){ return NO_SUCH_PAGE; } Graphics2D graphics2D = (Graphics2D) graphics; Font font = new Font("宋体", Font.PLAIN, 5); graphics2D.setFont(font); drawString(graphics2D, "//////////////////////////////", 10, 17, 119, 8); font = new Font("宋体", Font.PLAIN, 7); graphics2D.setFont(font); int yIndex = 30; int lineHeight = 10; int linewidth = 120; Color defaultColor = graphics2D.getColor(); Color grey = new Color(145, 145, 145); //收货信息 yIndex = drawString(graphics2D, "收货人:路人甲", 10, yIndex, linewidth, lineHeight); yIndex = drawString(graphics2D, "收货地址:北京市海淀区上地十街10号百度大厦", 10, yIndex + lineHeight, linewidth, lineHeight); //收货信息边框 stroke stroke = new Basicstroke(0.5f, Basicstroke.CAP_BUTT, Basicstroke.JOIN_BEVEL,0,new float[]{4, 4},0); graphics2D.setstroke(stroke); graphics2D.drawRect(5, 10, 129, yIndex); //药店名称 linewidth = 129; lineHeight = 8; graphics2D.setFont(new Font("宋体", Font.BOLD, 8)); graphics2D.setColor(defaultColor); yIndex = drawString(graphics2D, "北京药店零售小票", 5, yIndex + lineHeight + 20, linewidth, 12); graphics2D.setFont(new Font("宋体", Font.PLAIN, 6)); graphics2D.setColor(grey); yIndex = drawString(graphics2D, "操作员:小清新", 5, yIndex + lineHeight + 2, linewidth, lineHeight); yIndex = drawString(graphics2D, "日期:2017-01-05", 5 + linewidth/2, yIndex, linewidth, lineHeight); yIndex = drawString(graphics2D, "品名", 5, yIndex + lineHeight * 2 - 5, linewidth, lineHeight); yIndex = drawString(graphics2D, "规格", (linewidth/10)*4, yIndex, linewidth, lineHeight); yIndex = drawString(graphics2D, "单价", (linewidth/10)*8, yIndex, linewidth, lineHeight); yIndex = drawString(graphics2D, "数量", (linewidth/10)*10, yIndex, linewidth, lineHeight); for (int i=0; ilinewidth){ graphics2D.drawString(sb.toString(), x, y); sb.setLength(0); y = y + lineHeight; charsWidth = fontMetrics.charWidth(chars[i]); sb.append(chars[i]); } else{ charsWidth = charsWidth + fontMetrics.charWidth(chars[i]); sb.append(chars[i]); } } if(sb.length()>0){ graphics2D.drawString(sb.toString(), x, y); y = y + lineHeight; } return y - lineHeight; } } }

运行结果:

效果预览:

总结

简单说就是编写一段Java程序,将输出结果另存为“ *.xps  ”格式文件,由打印机输出,非常简单。希望对大家有所帮助。如有问题欢迎留言指出。感谢朋友们对本站的支持

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...