JAVA调用小票打印机

String[] pris = new String[4];

int height = 175 + pris.length * 15 + 20;

// 通俗理解就是书、文档
Book book = new Book();

// 打印格式
PageFormat pf = new PageFormat();
pf.setOrientation(PageFormat.PORTRAIT);

// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
Paper p = new Paper();
p.setSize(230, height);
p.setImageableArea(5, -20, 230, height + 20);
pf.setPaper(p);

// 把 PageFormat 和 Printable 添加到书中,组成一个页面
book.append(new Prient(line, waitNum), pf);

// 获取打印服务对象
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(book);
try {
job.print();
} catch (PrinterException e) {
System.out.println(“================打印出现异常”);

}
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 com.mohe.common.clock.JClock;
import com.mohe.models.LineUP;
import com.mohe.utils.date.DateTimeUtils;

/**
* 打印类
*
* @author dxm
*
*/
public class Prient implements Printable {

private LineUP line = null;
private int waitNum = 0;

/**
* 构造器
*
* @param line
*/
public Prient(LineUP line, int waitNum) {
this.line = line;
this.waitNum = waitNum;
}

@Override
public int print(Graphics g, PageFormat pf, int page) throws PrinterException {

if (page > 0) {
return NO_SUCH_PAGE;
}

String[] pris = Function.prm.prientStr.split(“\n”);

Graphics2D g2d = (Graphics2D) g;
g2d.setFont(new Font(“Default”, Font.PLAIN, 14));
g2d.drawString(“等位排单号”, 50, 10);
g2d.drawString(“————————————-“, 7, 20);
g2d.drawString(“手机号码:” + line.tel, 7, 35);
g2d.drawString(“就餐人数:” + line.repastNum, 7, 50);
g2d.drawString(“领号日期:” + DateTimeUtils.get19Time(line.commitTime), 7, 65);
g2d.drawString(“————————————-“, 7, 80);
g2d.setFont(new Font(“Default”, Font.PLAIN, 25));
g2d.drawString(Function.getDeskType(line.deskType) + line.code + “号”, 7, 105);
g2d.setFont(new Font(“Default”, Font.PLAIN, 14));
g2d.drawString(“您之前还有” + waitNum + “桌客人在等待”, 7, 130);
g2d.drawString(“————————————-“, 7, 145);
g2d.drawString(“*打印时间:” + DateTimeUtils.get19Time(JClock.getDate()) + “*”, 7, 160);
for (int i = 0; i < pris.length; i++) {
String s = pris[i];
g2d.drawString(“*” + s + “*”, 7, 175 + 15 * i);
}
g2d.drawString(“店名:” + Function.res.name, 7, 175 + pris.length * 15);

return PAGE_EXISTS;
}
}

作者:daixinmei 发表于2014-3-13 15:46:30 原文链接

Tagged:

Comments are closed.