编程语言
首页 > 编程语言> > java调用本地打印机,绘制打印模板,小票模板

java调用本地打印机,绘制打印模板,小票模板

作者:互联网

文章目录

jar资源

网盘链接:https://pan.baidu.com/s/1fFvKpiwwva2gZl-WLfsY9A
密钥:q6wh

绘制打印模板

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.util.ArrayList;
import java.util.List;

/**
 * 模板
 * @author admin
 * 
 */
public class Prient implements Printable {
	// 菜品集合
	public static List<Test> testList = new ArrayList<Test>();

	// 设置小票打印
	public int print(Graphics g, PageFormat pf, int page)
			throws PrinterException {
		if (page > 0) {
			return NO_SUCH_PAGE;
		}
		Graphics2D g2d = (Graphics2D) g;
		// 设置颜色
		g2d.setColor(Color.BLACK);
		//模式  字体   字体大小
		g2d.setFont(new Font("Default", Font.PLAIN, 16));
		// 参数1:显示内容 参数2:横向偏移 参数3:纵向偏移
		g2d.drawString("点菜清单", 100, 50);
		g2d.drawString("------------------------------------------------", 40, 70);
		g2d.setFont(new Font("Default", Font.PLAIN, 12));
		g2d.drawString("点餐员:自定义", 40, 90);
		g2d.drawString("电话:自定义", 40, 110);
		g2d.drawString("用餐时间:自定义", 40, 130);
		g2d.drawString("用餐地址:打印测试", 40, 150);
		g2d.setFont(new Font("Default", Font.PLAIN, 16));
		g2d.drawString("------------------------------------------------", 40, 170);
		g2d.drawString("菜品             单价             小计", 40, 190);
		g2d.setFont(new Font("Default", Font.PLAIN, 12));
		int H1 = 190;
		double zmoney = 0;
		int count = 0;
		for (Test test : testList) {
			count = count + 1;
			H1 = H1 + 20;
			zmoney = test.getMoney() * test.getNum() + zmoney;
			g2d.drawString(count + "." + test.getName() + "(" + test.getNum()
					+ "份)     ¥" + test.getMoney()+"     ¥"+test.getMoney()*test.getNum(), 40, H1);
		}
		g2d.setFont(new Font("Default", Font.PLAIN, 16));
		g2d.drawString("------------------------------------------------", 40, H1 + 20);
		g2d.setFont(new Font("Default", Font.PLAIN, 12));
		g2d.drawString("合计:¥" + zmoney, 40, H1 + 40);
		g2d.drawString("优惠金额:¥" + 20, 40, H1 + 60);
		g2d.drawString("应收:¥" + (zmoney-20), 40, H1 + 80);
		g2d.drawString("实收:¥" + zmoney, 40, H1 + 100);
		g2d.drawString("找零:¥" + 20, 40, H1 + 120);
		g2d.drawString("收银员:" + "打印测试", 40, H1 + 140);
		g2d.drawString("谢谢惠顾,欢迎下次光临!", 80, H1 + 160);
		return PAGE_EXISTS;
	}
	public static List<Test> getTestList() {
		return testList;
	}
	public static void setTestList(List<Test> testList) {
		Prient.testList = testList;
	}
}

自定义商品对象

/**
 * 菜品对象
 * 
 * @author admin
 * 
 */
public class Test {
	// 菜品名称
	private String name;
	// 价格
	private double money;
	// 数量
	private Integer num;
	//菜品分类
	private String fenlei;

	public Test() {
		super();
	}
	public Test(String name, double money, Integer num) {
		super();
		this.name = name;
		this.money = money;
		this.num = num;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getMoney() {
		return money;
	}
	public void setMoney(double money) {
		this.money = money;
	}
	public Integer getNum() {
		return num;
	}
	public void setNum(Integer num) {
		this.num = num;
	}
	public String getFenlei() {
		return fenlei;
	}
	public void setFenlei(String fenlei) {
		this.fenlei = fenlei;
	}
}

调用打印机

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;
import java.util.ArrayList;
import java.util.List;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.standard.PrinterName;

/**
 * 打印测试类
 * 
 * @author admin
 * 
 */
public class MainTest {

	public static void main(String[] args) {
		//这里是测试用的数据,自己定义就好了
		List<Test> testList = new ArrayList<Test>();
		Test t1 = new Test("麻辣牛肉", 23.00, 2);
		Test t2 = new Test("麻辣牛肉", 23.00, 4);
		Test t3 = new Test("精品千层肚", 24.00, 3);
		Test t4 = new Test("麻辣牛肉", 23.00, 2);
		Test t5 = new Test("极品鲜毛肚", 26.00, 1);
		Test t6 = new Test("极品鲜毛肚", 26.00, 1);
		Test t7 = new Test("极品鲜毛肚", 26.00, 1);
		Test t8 = new Test("极品鲜毛肚", 26.00, 1);
		Test t9 = new Test("极品鲜毛肚", 26.00, 1);
		testList.add(t1);
		testList.add(t2);
		testList.add(t3);
		testList.add(t4);
		testList.add(t5);
		testList.add(t6);
		testList.add(t7);
		testList.add(t8);
		testList.add(t9);
		
		// 设置小票模型菜品集合
		Prient.setTestList(testList);
		// 定义纸张高度
		int height = 200 + (testList.size() * 20) + 160;
		// 通俗理解就是书、文档
		Book book = new Book();
		// 打印格式
		PageFormat pf = new PageFormat();
		// 原点在纸张的左上方,x 指向右方,y 指向下方。
		pf.setOrientation(PageFormat.PORTRAIT);
		// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
		Paper p = new Paper();
		// 页面宽度 页面高度
		p.setSize(260, height);
		// x轴 y轴 宽度 高度
		p.setImageableArea(0, 0, 260, height);
		pf.setPaper(p);
		// 把 PageFormat 和 Printable 添加到书中,组成一个页面
		book.append(new Prient(), pf);
		// 指定打印机打印(printerName打印机名称)
		HashAttributeSet hs = new HashAttributeSet();
		String printerName = "OneNote";
		hs.add(new PrinterName(printerName, null));
		PrintService[] pss = PrintServiceLookup.lookupPrintServices(null, hs);
		if (pss.length == 0) {
			System.out.println("无法找到打印机:" + printerName);
			return;
		}
		// 获取打印服务对象
		PrinterJob job = PrinterJob.getPrinterJob();
		// 写入书
		job.setPageable(book);
		try {
			Integer random6 = (int) ((Math.random() * 9 + 1) * 100000);
			System.out.println("Z"+System.currentTimeMillis()+random6.toString());
			// 添加指定的打印机
			job.setPrintService(pss[0]);
			// 打印执行
			job.print();
		} catch (PrinterException e) {
			System.out.println("================打印出现异常");
		}
	}
}

标签:java,小票,testList,g2d,import,Test,new,public,模板
来源: https://blog.csdn.net/qq_41998978/article/details/98944741