Java調用熱敏票據打印機打印小票

package com.yc.printer;


/**
 * 源辰信息
 * 商品信息 
 * @author navy
 * 2017年5月23日
 */
public class GoodsInfo {
private String gname; //商品名稱
private String price; //商品單價
private String num; //商品數量
private String total; //小計

@Override
public String toString() {
return "GoodsInfo [gname=" + gname + ", price=" + price + ", num=" + num + ", total=" + total + "]";
}


public GoodsInfo(String gname, String price, String num, String total) {
super();
this.gname = gname;
this.price = price;
this.num = num;
this.total = total;
}


public String getGname() {
return gname;
}


public void setGname(String gname) {
this.gname = gname;
}


public String getPrice() {
return price;
}


public void setPrice(String price) {
this.price = price;
}


public String getNum() {
return num;
}


public void setNum(String num) {
this.num = num;
}


public String getTotal() {
return total;
}


public void setTotal(String total) {
this.total = total;
}


@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((gname == null) ? 0 : gname.hashCode());
result = prime * result + ((num == null) ? 0 : num.hashCode());
result = prime * result + ((price == null) ? 0 : price.hashCode());
result = prime * result + ((total == null) ? 0 : total.hashCode());
return result;
}


@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
GoodsInfo other = (GoodsInfo) obj;
if (gname == null) {
if (other.gname != null)
return false;
} else if (!gname.equals(other.gname))
return false;
if (num == null) {
if (other.num != null)
return false;
} else if (!num.equals(other.num))
return false;
if (price == null) {
if (other.price != null)
return false;
} else if (!price.equals(other.price))
return false;
if (total == null) {
if (other.total != null)
return false;
} else if (!total.equals(other.total))
return false;
return true;
}
}




package com.yc.printer;


import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * 源辰信息
 * 銷售清單對象
 * @author navy
 * 2017年5月23日
 */
public class SalesTicket  implements Printable{
private List<GoodsInfo> goods; //商品列表
private String operatorName="源辰信息"; //操作員
private String orderId; //訂單編號
private String totalGoodsNum; //商品總數
private String totalPrice; //總金額
private String actualCollection; //實收款
private String giveChange; //找零


private String cardNumber; //會員編號
private String integral; //積分

private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss EEE");


@Override
public String toString() {
return "SalesTicket [goods=" + goods + ", operatorName=" + operatorName + ", orderId=" + orderId
+ ", totalGoodsNum=" + totalGoodsNum + ", totalPrice=" + totalPrice + ", actualCollection="
+ actualCollection + ", giveChange=" + giveChange + ", cardNumber=" + cardNumber + ", integral="
+ integral + "]";
}


public SalesTicket(List<GoodsInfo> goods, String operatorName, String orderId, String totalGoodsNum,
String totalPrice, String actualCollection, String giveChange, String cardNumber, String integral) {
super();
this.goods = goods;
this.operatorName = operatorName;
this.orderId = orderId;
this.totalGoodsNum = totalGoodsNum;
this.totalPrice = totalPrice;
this.actualCollection = actualCollection;
this.giveChange = giveChange;
this.cardNumber = cardNumber;
this.integral = integral;
}


public SalesTicket(List<GoodsInfo> goods, String operatorName, String orderId, String totalGoodsNum,
String totalPrice, String actualCollection, String giveChange) {
super();
this.goods = goods;
this.operatorName = operatorName;
this.orderId = orderId;
this.totalGoodsNum = totalGoodsNum;
this.totalPrice = totalPrice;
this.actualCollection = actualCollection;
this.giveChange = giveChange;
}


/**
* 打印方法
* graphics - 用來繪製頁面的上下文,即打印的圖形
* pageFormat - 將繪製頁面的大小和方向,即設置打印格式,如頁面大小一點爲計量單位(以1/72 英寸爲單位,1英寸爲25.4毫米。A4紙大致爲595 × 842點)
* 小票紙寬度一般爲58mm,大概爲165點
* pageIndex - 要繪製的頁面從 0 開始的索引 ,即頁號
*/
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
//此 Graphics2D 類擴展 Graphics 類,以提供對幾何形狀、座標轉換、顏色管理和文本佈局更爲複雜的控制。
//它是用於在 Java(tm) 平臺上呈現二維形狀、文本和圖像的基礎類。 
Graphics2D g2 = (Graphics2D) graphics;  


g2.setColor(Color.black);//設置打印顏色爲黑色  


//打印起點座標  
double x= pageFormat.getImageableX();  //返回與此 PageFormat相關的 Paper對象的可成像區域左上方點的 x座標。  
double y= pageFormat.getImageableY();  //返回與此 PageFormat相關的 Paper對象的可成像區域左上方點的 y座標。


//Font.PLAIN: 普通樣式常量  Font.ITALIC 斜體樣式常量 Font.BOLD 粗體樣式常量。
Font font = new Font("宋體",Font.BOLD,10); //根據指定名稱、樣式和磅值大小,創建一個新 Font。

g2.setFont(font);//設置標題打印字體     

float heigth = font.getSize2D();//獲取字體的高度  

//設置小票的標題標題  
g2.drawString("源辰信息科技有限公司",(float)x+25,(float)y+heigth); 

float line = 2*heigth; //下一行開始打印的高度
g2.setFont(new Font("宋體", Font.PLAIN,8));//設置正文字體  
heigth = font.getSize2D();// 字體高度  

line+=2;
//設置操作員 
g2.drawString("操作員:"+operatorName,(float)x+20,(float)y+line); 
line+=heigth;

//設置訂單號  
g2.drawString("訂單號:"+orderId, (float)x+20,(float)y+line);  
line+=heigth+2; 

//設置標題  
g2.drawString("名稱",(float)x+20, (float)y+line);  
g2.drawString("單價",(float)x+60, (float)y+line);  
g2.drawString("數量",(float)x+90, (float)y+line);  
g2.drawString("小計",(float)x+120, (float)y+line);  
line+=heigth;
/*
* 虛線繪製設置    setStroke(Stroke):爲 Graphics2D 上下文設置 Stroke
* 由 BasicStroke定義的呈現屬性描述了用畫筆沿 Shape 的輪廓繪製的某個標記的形狀,以及應用在 Shape 路徑線段的末端和連接處的裝飾。
* 這些呈現屬性包括: 
* width:畫筆的寬度,是垂直於畫筆軌跡的測量值。  此寬度必須大於或等於 0.0f,0.0f爲最細線條。  
* end caps:在未封閉子路徑和虛線線段的末端應用的一些裝飾。如果子路徑沒有 CLOSE 段,則在同一點上開始和結束的子路徑仍被認爲是未封閉的。
* 關於 CLOSE 段的更多信息,請參閱 SEG_CLOSE。三個不同的裝飾是:
* CAP_BUTT:無裝飾地結束未封閉的子路徑和虛線線段。
* CAP_ROUND:使用半徑等於畫筆寬度一半的圓形裝飾結束未封閉的子路徑和虛線線段。
* CAP_SQUARE:使用正方形結束未封閉的子路徑和虛線線段,正方形越過線段端點,並延長等於線條寬度一半的距離。 
* line joins:在兩個路徑線段的交匯處,以及使用 SEG_CLOSE 封閉的子路徑端點的交匯處應用的裝飾。
* 三個不同的裝飾是:
* JOIN_BEVEL:通過直線連接寬體輪廓的外角,將路徑線段連接在一起。 
* JOIN_MITER:擴展路徑線段的外邊緣,直到它們連接在一起。 
* JOIN_ROUND:通過捨去半徑爲線長的一半的圓角,將路徑線段連接在一起。 
* miter limit:對剪裁具有 JOIN_MITER 裝飾的線接合點的限制。當斜接長度與筆劃寬度的比大於 miterlimit 值時,需要剪裁線接合點。
* 斜接長度是斜接的對角線長度,即交匯處的內棱角和外棱角之間的距離。兩條線段形成的角度越小,斜接長度就越長,交匯處的角度就越尖銳。
* 默認 miterlimit 值爲 10.0f,它會使所有小於 11 度的角都被剪裁。剪裁斜接會使線接合點的裝飾變成斜角。 必須大於或等於 1.0f。
* dash attributes:關於如何通過在不透明和透明部分之間交替形成一個虛線模式的定義。 表示虛線模式的數組  
* dash phase - 開始虛線模式的偏移量  
*/
//虛線設置
g2.setStroke(new BasicStroke(1f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER,4.0f,new float[]{4.0f},0.0f));  
//在此圖形上下文的座標系中使用當前顏色在點 (x1, y1) 和 (x2, y2) 之間畫一條線。 即繪製虛線
g2.drawLine((int)x,(int)(y+line),(int)x+158,(int)(y+line));  
line+=heigth; 
//設置商品清單
if(goods!=null && goods.size()>0){
for(GoodsInfo gdf:goods){
g2.drawString(gdf.getGname(),(float)x+15, (float)y+line);  
g2.drawString(gdf.getPrice(),(float)x+60, (float)y+line);  
g2.drawString(gdf.getNum(),(float)x+95,(float)y+line);  
g2.drawString(gdf.getTotal(),(float)x+120,(float)y+line);  
line += heigth; 
}
}
g2.drawLine((int) x, (int)(y+line), (int) x + 158, (int)(y+line));  
line += heigth+2;  
g2.drawString("商品總數:"+totalGoodsNum+ "件",(float)x+15,(float)y+line);  
g2.drawString("合計:"+totalPrice+" 元", (float)x+80, (float)y+line);  
line += heigth;  
g2.drawString("實收:"+actualCollection+"元",(float)x+15,(float)y+line);  
g2.drawString("找零:"+giveChange+"元",(float)x+80,(float)y+line);  
line += heigth;

if(cardNumber!=null && !"".equals(cardNumber)){
g2.drawString("當前會員:"+cardNumber,(float)x+15,(float)y+line);
line += heigth;
g2.drawString("積分:"+integral,(float)x+15,(float)y+line);  
}
g2.drawString("時間:"+sdf.format(new Date()),(float)x+15,(float)y+line);  
line += heigth; 
g2.drawString("錢票請當面點清,離開櫃檯恕不負責",(float)x+15,(float)y+line);  
switch (pageIndex) {  
case 0:  
return PAGE_EXISTS;  //0 
default:  
return NO_SUCH_PAGE;   //1
}  
}
}


package com.yc.printer;


import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;


/**
 * 源辰信息
 * 打印對象 -> 實現Printable接口即可調用打印機打印
 * @author navy
 * 2017年5月23日
 */
public class YcPrinter{
private SalesTicket salesTicket;

public YcPrinter(SalesTicket salesTicket){
this.salesTicket=salesTicket;
}

    public void printer() {  
        try {  
            //Book 類提供文檔的表示形式,該文檔的頁面可以使用不同的頁面格式和頁面 painter
            Book book = new Book(); //要打印的文檔
            
            //PageFormat類描述要打印的頁面大小和方向  
            PageFormat pf = new PageFormat();  //初始化一個頁面打印對象
            pf.setOrientation(PageFormat.PORTRAIT); //設置頁面打印方向,從上往下,從左往右
          
            //設置打印紙頁面信息。通過Paper設置頁面的空白邊距和可打印區域。必須與實際打印紙張大小相符。  
            Paper paper = new Paper(); 
            paper.setSize(158,30000);// 紙張大小  
            paper.setImageableArea(0,0,158,30000);// A4(595 X 842)設置打印區域,其實0,0應該是72,72,因爲A4紙的默認X,Y邊距是72  
            pf.setPaper(paper);  
  
            book.append(salesTicket,pf);  
           
            PrinterJob job = PrinterJob.getPrinterJob();   //獲取打印服務對象  
            
            job.setPageable(book);  //設置打印類  
  
            job.print(); //開始打印 
        } catch (PrinterException e) {  
            e.printStackTrace();  
        }  
    }  
}




package com.yc.printer;


import java.util.ArrayList;
import java.util.List;


public class Test {
public static void main(String[] args) {
List<GoodsInfo> goods=new ArrayList<GoodsInfo>();
goods.add(new GoodsInfo("J2EE","11800","1","11800"));
goods.add(new GoodsInfo("大數據","14800","1","14800"));
goods.add(new GoodsInfo("前端","11800","1","11800"));

SalesTicket stk=new SalesTicket(goods,"源辰信息","201705230010","3","38400","38400","0");
YcPrinter p=new YcPrinter(stk);
p.printer();
}

}



發佈了70 篇原創文章 · 獲贊 78 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章