package hlsoft;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.ArrayList;
import java.util.List;
import hlsoft.PrintModel;
/**
* 通用热敏打印机类
*/
public class Prient implements Printable {
/**
* 打印内容主题
*/
private List<PrintModel> listModel;
/**
* 小票标题
*/
private String title;
/**
* 打印纸高度
*/
private Integer height;
/**
* 标题距左距离
*/
private Integer marginLeft = 50;
/**
* 标题距上距离
*/
private Integer marginTop = 10;
public int commonPrint() {
int height = 0 + listModel.size() * 20 + this.height;
// 通俗理解就是书、文档
Book book = new Book();
// 打印格式
PageFormat pf = new PageFormat();
pf.setOrientation(PageFormat.PORTRAIT);
// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
Paper p = new Paper();
p.setSize(113, 85);
p.setImageableArea(2, 2, 113, 85);
pf.setPaper(p);
// 把 PageFormat 和 Printable 添加到书中,组成一个页面
book.append(new Prient(this.listModel, this.title), pf);
// 获取打印服务对象
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(book);
try {
job.print();
} catch (PrinterException e) {
System.out.println("================打印出现异常");
}
return 0;
}
@Override
public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
if (page > 0) {
return NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D) g;
/*g2d.setFont(new Font(title, Font.PLAIN, 12));
g2d.drawString(title, marginLeft, marginTop);*/
g2d.setFont(new Font(title, Font.PLAIN, 8));
int i = 1;
//g2d.drawString("--------------------------------------------", 7, 20);
for (PrintModel m : listModel) {
/*if (!m.getIsWrap()) {
if (m.getName()!=null&&!"".equals(m.getName())) {
g2d.drawString(m.getName() + m.getVal(), 7, 30 + i * 12);
System.out.println(i);
} else if (null != m.getImage()) {
g2d.drawImage(m.getImage(), 7, 7 + i * 12, null);
//i += 8;
System.out.println(i);
}
} else {
g2d.drawString(m.getName(), 7, 30 + i * 12);
i++;
g2d.drawString(m.getVal(), 7, 30 + i * 12);
}
i++;*/
if (m.getName()!=null&&!"".equals(m.getName())) {
g2d.drawString(m.getName() + m.getVal(), m.getX(), m.getY());
} else if (null != m.getImage()) {
g2d.drawImage(m.getImage(), m.getX(), m.getY(), null);
}
}
return PAGE_EXISTS;
}
public static void main(String[] args) {
List<PrintModel> oo = new ArrayList<PrintModel>();
oo.add(new PrintModel("编号1", "0055726"));
try {
oo.add(new PrintModel(new QRCodeEvents().writeQrCodeContent("text", "D:/")));
} catch (Exception e1) {
}
Prient p = new Prient(oo, "运输单", 340);
p.commonPrint();
}
public Prient(List<PrintModel> listModel, String title, Integer height, Integer marginLeft, Integer marginTop) {
this.listModel = listModel;
this.title = title;
this.height = height;
this.marginLeft = marginLeft;
this.marginTop = marginTop;
}
public Prient(List<PrintModel> listModel, String title, Integer height) {
this.listModel = listModel;
this.title = title;
this.height = height;
}
public Prient(List<PrintModel> listModel, String title) {
this.listModel = listModel;
this.title = title;
}
}