重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

JAVA如何实现连接本地打印机并打印文件

小编给大家分享一下JAVA如何实现连接本地打印机并打印文件,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

成都创新互联公司提供成都网站制作、网站建设、外贸网站建设、网页设计,品牌网站设计一元广告等致力于企业网站建设与公司网站制作,十多年的网站开发和建站经验,助力企业信息化建设,成功案例突破上1000家,是您实现网站建设的好选择.

实现代码一

import javax.print.*;import javax.print.attribute.DocAttributeSet;import javax.print.attribute.HashDocAttributeSet;import javax.print.attribute.HashPrintRequestAttributeSet;import javax.print.attribute.PrintRequestAttributeSet;import java.io.File;import java.io.FileInputStream;public class PrintDemo1 {  public void printPdf(String fileName) {    //构造一个文件选择器,默认为当前目录    File file = new File(fileName);//获取选择的文件    //构建打印请求属性集    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();    //设置打印格式,因为未确定文件类型,这里选择AUTOSENSE    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;    //查找所有的可用打印服务    PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);    //定位默认的打印服务    //PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();    // 显示打印对话框    PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras);    if (service != null) {      try {        DocPrintJob job = service.createPrintJob(); // 创建打印作业        FileInputStream fis; // 构造待打印的文件流        fis = new FileInputStream(file);        DocAttributeSet das = new HashDocAttributeSet();        Doc doc = new SimpleDoc(fis, flavor, das);        job.print(doc, pras);      } catch (Exception e) {        e.printStackTrace();      }    }  }  public static void main(String args[]) {    PrintDemo1 pic = new PrintDemo1();    pic.printPdf("F:\\java资源2\\Docker视频教程\\赠送3-从Docker到Kubernetes之技术实战\1.为什么你需要学习Docker\1.pdf");  }}

代码二

package com.iba.cxx.adm.controller;import javax.print.*;import javax.print.attribute.DocAttributeSet;import javax.print.attribute.HashDocAttributeSet;import javax.print.attribute.HashPrintRequestAttributeSet;import javax.swing.*;import java.io.File;import java.io.FileInputStream;/** * Created by Administrator on 2017/9/8 0008. */public class TestController {  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:/haha.txt"); //获取选择的文件      File file = fileChooser.getSelectedFile();//获取选择的文件      //构建打印请求属性集      HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();      //设置打印格式,因为未确定类型,所以选择autosense      DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;      //查找所有的可用的打印服务      PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);      //定位默认的打印服务      PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();      //显示打印对话框      PrintService service = ServiceUI.printDialog(null, 200, 200, printService,          defaultService, flavor, pras);      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);        } catch (Exception e) {          e.printStackTrace();        }      }    }  }}

以上是“JAVA如何实现连接本地打印机并打印文件”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


本文标题:JAVA如何实现连接本地打印机并打印文件
标题URL:http://cqcxhl.com/article/ipgiis.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP