簡單介紹運行環境:
語言: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程序,將輸出結果另存為“ *.xps ”格式文件,由打印機輸出,非常簡單。希望對大家有所幫助。如有問題歡迎留言指出。感謝朋友們對本站的支持。
原文鏈接:http://www.cnblogs.com/rexfang/p/7441146.html