首页 > 其他 > 详细

JAVA调用打印机打印指定文件

时间:2014-01-17 00:16:18      阅读:640      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
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;
public class Testa {

    
        /**
         * 调用打印机功能
         * @author Administrator
         *
         */
        
        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);
            
            String fileName=  "D:" + File.separator + "zkyzl.txt" ;
            
            if(service != null){
                try {
                    DocPrintJob pj =service.createPrintJob();
                    FileInputStream fis = new FileInputStream(fileName);
                    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("打印失败");
            }
        }
    }
View Code

JAVA调用打印机打印指定文件

原文:http://www.cnblogs.com/summer520/p/3522253.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!