Java編程打印購物小票實現(xiàn)代碼

簡單介紹運行環(huán)境:

成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比弓長嶺網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式弓長嶺網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋弓長嶺地區(qū)。費用合理售后完善,十多年實體公司更值得信賴。

語言:Java

工具:eclipse

系統(tǒng):Windows7

(打印設(shè)備暫時沒有,所以只能提供預(yù)覽圖)

最近,項目需要為商城做一個購物小票的打印功能,日常我們?nèi)コ匈I東西,結(jié)賬的時候收銀員都會打印一個小票,一般的商城也都需要這樣的一個小功能,本文給出的 demo 是在 58mm 的熱敏打印機下的例子,如果是其他紙張類型的打印機,調(diào)整紙張寬度即可。

package test;
import java.awt.*;
import java.awt.print.*;
/**
 * 打印機測試類(58mm)
 * 1、目標(biāo)打印機必須設(shè)置為默認打印機
 * 2、打印頁面的寬度和具體的打印機有關(guān),一般為打印紙的寬度,需要配置成系統(tǒng)參數(shù)
 * 3、一個漢字的寬度大概是12點
 */
public class PrintTest {
	public static void main(String[] args){
		if(PrinterJob.lookupPrintServices().length>0){
			/*
        打印格式
       */
			PageFormat pageFormat = new PageFormat();
			//設(shè)置打印起點從左上角開始,從左到右,從上到下打印
			pageFormat.setOrientation(PageFormat.PORTRAIT);
			/*
        打印頁面格式設(shè)置
       */
			Paper paper = new Paper();
			//設(shè)置打印寬度(固定,和具體的打印機有關(guān))和高度(跟實際打印內(nèi)容的多少有關(guān))
			paper.setSize(140, 450);
			//設(shè)置打印區(qū)域 打印起點坐標(biāo)、打印的寬度和高度
			paper.setImageableArea(0, 0, 135, 450);
			pageFormat.setPaper(paper);
			//創(chuàng)建打印文檔
			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, "http://////////////////////////////", 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, "收貨地址:北京市海淀區(qū)上地十街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, "規(guī)格", (lineWidth/10)*4, yIndex, lineWidth, lineHeight);
					yIndex = drawString(graphics2D, "單價", (lineWidth/10)*8, yIndex, lineWidth, lineHeight);
					yIndex = drawString(graphics2D, "數(shù)量", (lineWidth/10)*10, yIndex, lineWidth, lineHeight);
					for (int i=0; i<5; i++){
						graphics2D.setFont(new Font("宋體", Font.PLAIN, 7));
						yIndex = drawString(graphics2D, "E復(fù)合維生素B片100片E復(fù)合維生素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, "總  數(shù):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, "地址:北京市海淀區(qū)上地十街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("沒法發(fā)現(xiàn)打印機服務(wù)");
		}
	}
	/**
   * 字符串輸出
   * @param graphics2D  畫筆
   * @param text     打印文本
   * @param x       打印起點 x 坐標(biāo)
   * @param y       打印起點 y 坐標(biāo)
   * @param lineWidth   行寬
   * @param lineHeight  行高
   * @return 返回終點 y 坐標(biāo)
   */
	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;
		}
	}
}

運行結(jié)果:

Java編程打印購物小票實現(xiàn)代碼

效果預(yù)覽:

Java編程打印購物小票實現(xiàn)代碼

總結(jié)

簡單說就是編寫一段Java程序,將輸出結(jié)果另存為“ *.xps  ”格式文件,由打印機輸出,非常簡單。希望對大家有所幫助。如有問題歡迎留言指出。感謝朋友們對本站的支持。

網(wǎng)頁名稱:Java編程打印購物小票實現(xiàn)代碼
URL分享:http://bm7419.com/article2/igoeoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、、云服務(wù)器、商城網(wǎng)站、關(guān)鍵詞優(yōu)化、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計