当前位置 : 主页 > 编程语言 > java >

获取指定的打印机进行打印

来源:互联网 收集:自由互联 发布时间:2021-06-28
获取指定的打印机进行打印 package com.jxdd.order.service;import java.io.File;import java.io.FileInputStream;import javax.print.Doc;import javax.print.DocFlavor;import javax.print.DocPrintJob;import javax.print.PrintService;import
获取指定的打印机进行打印
package com.jxdd.order.service;

import java.io.File;
import java.io.FileInputStream;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
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.swing.JFileChooser;

/** 
 *
 * @author : ouyanglei: 
 * @date :2017年12月11日
 * @version 1.0 
 * @parameter    
 * @return  
 */
public class PrintDemo {   
    
    public static void main(String[] args) {   
        JFileChooser fileChooser = new JFileChooser(); //创建打印作业   
        int state = fileChooser.showOpenDialog(null);   
        if(state == fileChooser.APPROVE_OPTION){   
            File file = new File("D:/printTest.txt"); //获取选择的文件   
            //构建打印请求属性集   
            HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();   
            //设置打印格式,因为未确定类型,所以选择autosense   
            DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;   
            //查找所有的可用的打印服务   
            PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);   
            //将所有查找出来的打印机与自己想要的打印机进行匹配,找出自己想要的打印机  
            PrintService myPrinter = null;  
            for (int i = 0; i < printService.length; i++) {  
                System.out.println("service found: " + printService[i]);  
                String svcName = printService[i].toString();  
                if (svcName.contains("Snagit 11")) {  
                    myPrinter = printService[i];  
                    System.out.println("my printer found: " + svcName);  
                    System.out.println("my printer found: " + myPrinter);  
                    break;  
                }  
            }  
//            //定位默认的打印服务   
//            PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();   
//            //显示打印对话框   
//            PrintService service = ServiceUI.printDialog(null, 200, 200, printService,    
//                    defaultService, flavor, pras);   
            if(myPrinter != null){   
                try {   
                    DocPrintJob job = myPrinter.createPrintJob(); //创建打印作业   
                    FileInputStream fis = new FileInputStream(file); //构造待打印的文件流   
                    DocAttributeSet das = new HashDocAttributeSet();   
                    Doc doc = new SimpleDoc(fis, flavor, das);   
                    job.print(doc, pras);   
                } catch (Exception e) {   
                    e.printStackTrace();   
                }   
            }   
        }   
    }   
}
上一篇:跨域访问
下一篇:mybatis调用
网友评论