论坛首页 Web前端技术论坛

java实现调用默认打印机实现打印

浏览 7224 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (1)
作者 正文
   发表时间:2012-03-03  
接口 PrintService 是 DocPrintJob 的工厂。PrintService 描述了打印机的功能,并可查询它来了解打印机支持的属性。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

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.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
	/**
	 * 调用打印机功能
	 * @author Administrator
	 *
	 */
public class PrintDemoII {
	
	public static void main(String[] args) {
		DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
		PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
		PrintService []pservices = PrintServiceLookup.lookupPrintServices(flavor, aset);
		PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
		PrintService service = ServiceUI.printDialog(null, 200, 200, pservices, 
				defaultService, flavor, aset);
		if(service != null){
			try {
				DocPrintJob pj =service.createPrintJob();
				FileInputStream fis = new FileInputStream("D:" + File.separator + "zkyzl.txt");
				DocAttributeSet das = new HashDocAttributeSet();
				Doc doc = new SimpleDoc(fis, flavor, das);
				pj.print(doc, aset);
			} catch (FileNotFoundException fe) {
				fe.printStackTrace();
			} catch (PrintException e) {
				e.printStackTrace();
			}
		}else {
			System.out.println("打印失败");
		}
	}
}

官方详解:http://doc.java.sun.com/DocWeb/api/javax.print.PrintService
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics