package com.zoipuus.printer;
import java.awt.Color;
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.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.ServiceUI;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.standard.PrinterName;
import javax.swing.JFileChooser;
public class PrintDemo implements Printable {
public static void main(String[] args) {
// TODO Auto-generated method stub
// doPrintFile();
(new PrintDemo()).doPrintLabel("zoipuus", "2016-03-19 17:45", 1);
}
private int pageNumbers;
private String name;
private String finishTime;
// 打印指定文字
public void doPrintLabel(String name, String finishTime, int pageNumbers) {
// TODO Auto-generated method stub
this.pageNumbers = pageNumbers;
this.name = name;
this.finishTime = finishTime;
// 创建文档
Book book = new Book();
// 设置打印格式(PORTRAIT:竖向打印,LANDSCAPE:横向打印)
PageFormat pageFormat = new PageFormat();
pageFormat.setOrientation(PageFormat.PORTRAIT);
// 通过Paper设置页面的空白边距和可打印边距
Paper paper = new Paper();
// A4 Size : 590 * 840
paper.setSize(590, 840);
// print area
paper.setImageableArea(0, 0, 590, 840);
pageFormat.setPaper(paper);
// 把PageFormat和Printable添加到书中。组成一个页面
book.append(this, pageFormat, pageNumbers);
HashAttributeSet sets = new HashAttributeSet();
sets.add(new PrinterName("Code", null));
// 获取打印服务对象,打印格式为null
PrintService[] services = PrintServiceLookup.lookupPrintServices(null,
sets);
PrinterJob job = PrinterJob.getPrinterJob();
try {
if (services.length > 0) {
job.setPrintService(services[0]);
}
// 设置打印类
job.setPageable(book);
// print
job.print();
} catch (PrinterException e) {
e.printStackTrace();
}
}
// 打印指定文件
private static void doPrintFile() {
// TODO Auto-generated method stub
JFileChooser fileChooser = new JFileChooser();
fileChooser.showOpenDialog(null);
File file = fileChooser.getSelectedFile();
if (file != null) {
// 选择文件
// File file = new File("D:/TestPrinter.txt");
System.out.println("选择文件:" + file.toString());
// 构建打印请求属性集
HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
System.out.println("构建打印请求属性集");
// 设置打印格式,未确定类型,选择autosense
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
System.out.println("设置打印格式,未确定类型,选择autosense");
// 查找所有的可用的打印服务
PrintService printService[] = PrintServiceLookup
.lookupPrintServices(flavor, pras);
System.out.println("查找所有的可用的打印服务");
// 定位默认的打印服务
PrintService defaultService = PrintServiceLookup
.lookupDefaultPrintService();
System.out.println("定位默认的打印服务");
// 显示打印对话框
PrintService service = ServiceUI.printDialog(null, 200, 200,
printService, defaultService, flavor, pras);
System.out.println("显示打印对话框");
if (service != null) {
try {
// 创建打印业务
DocPrintJob job = service.createPrintJob();
FileInputStream fis = new FileInputStream(file);
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);
job.print(doc, pras);
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("FileNotFoundException");
e.printStackTrace();
} catch (PrintException e) {
// TODO Auto-generated catch block
System.out.println("PrintException");
e.printStackTrace();
}
} else {
System.out.println("service = null");
}
}
}
@Override
public int print(Graphics arg0, PageFormat format, int pageIbdex)
throws PrinterException {
// TODO Auto-generated method stub
Graphics2D g2 = (Graphics2D) arg0;
// 设置颜色
g2.setColor(Color.black);
Font font = new Font("叶根友毛笔行书2.0版", Font.PLAIN, 24);
g2.setFont(font);
if (pageIbdex >= this.pageNumbers)
return NO_SUCH_PAGE;
g2.drawString(name, 134, 145);
Font font1 = new Font("hakuyoxingshu7000", Font.PLAIN, 18);
g2.setFont(font1);
g2.drawString(finishTime, 266, 145);
return 0;
}
}