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

时间:2022-12-01 22:06:48

简单介绍运行环境:

语言:java

工具:eclipse

系统:windows7

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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; i<5; i++){
                        graphics2d.setfont(new font("宋体", font.plain, 7));
                        yindex = drawstring(graphics2d, "e复合维生素b片100片e复合维生素b片100片", 5, yindex + 15, (linewidth/10)*7, 10);
                        graphics2d.setfont(new font("宋体", font.plain, 6));
                        graphics2d.setcolor(grey);
                        yindex = drawstring(graphics2d, "100片/盒", 5, yindex + 11, linewidth, lineheight);
                        yindex = drawstring(graphics2d, "14.50", (linewidth/10)*8, yindex, linewidth, lineheight);
                        yindex = drawstring(graphics2d, "2", (linewidth/10)*10, yindex, linewidth, lineheight);
                        graphics2d.setfont(new font("宋体", font.plain, 7));
                        yindex = yindex + 2;
                        graphics2d.drawline(5, yindex, 5 + linewidth, yindex);
                    }
                    graphics2d.setcolor(defaultcolor);
                    yindex = drawstring(graphics2d, "会员名称:小清新", 5, yindex + lineheight * 2, linewidth, lineheight);
                    yindex = drawstring(graphics2d, "总  数:6", 5, yindex + lineheight, linewidth, lineheight);
                    yindex = drawstring(graphics2d, "总  计:55.30", 5, yindex + lineheight, linewidth, lineheight);
                    yindex = drawstring(graphics2d, "收  款:100.00", 5, yindex + lineheight, linewidth, lineheight);
                    yindex = drawstring(graphics2d, "找  零:44.70", 5, yindex + lineheight, linewidth, lineheight);
                    graphics2d.setfont(new font("宋体", font.plain, 6));
                    graphics2d.setcolor(grey);
                    yindex = drawstring(graphics2d, "电话:020-123456", 5, yindex + lineheight * 2, linewidth, lineheight);
                    yindex = drawstring(graphics2d, "地址:北京市海淀区上地十街10号百度大厦", 5, yindex + lineheight, linewidth, lineheight);
                    yindex = yindex + 20;
                    graphics2d.drawline(0, yindex, 140, yindex);
                    return page_exists;
                }
            }
            , pageformat);
            //获取默认打印机
            printerjob printerjob = printerjob.getprinterjob();
            printerjob.setpageable(book);
            try {
                printerjob.print();
            }
            catch (printerexception e) {
                e.printstacktrace();
                system.out.println("打印异常");
            }
        } else{
            system.out.println("没法发现打印机服务");
        }
    }
    /**
   * 字符串输出
   * @param graphics2d  画笔
   * @param text     打印文本
   * @param x       打印起点 x 坐标
   * @param y       打印起点 y 坐标
   * @param linewidth   行宽
   * @param lineheight  行高
   * @return 返回终点 y 坐标
   */
    private static int drawstring(graphics2d graphics2d, string text, int x, int y, int linewidth, int lineheight){
        fontmetrics fontmetrics = graphics2d.getfontmetrics();
        if(fontmetrics.stringwidth(text)<linewidth){
            graphics2d.drawstring(text, x, y);
            return y;
        } else{
            char[] chars = text.tochararray();
            int charswidth = 0;
            stringbuffer sb = new stringbuffer();
            for (int i=0; i<chars.length; i++){
                if((charswidth + fontmetrics.charwidth(chars[i]))>linewidth){
                    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编程打印购物小票实现代码

效果预览:

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

总结

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

原文链接:http://www.cnblogs.com/rexfang/p/7441146.html