JAVA如何实现连接本地打印机并打印文件
发表于:2024-10-27 作者:千家信息网编辑
千家信息网最后更新 2024年10月27日,小编给大家分享一下JAVA如何实现连接本地打印机并打印文件,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!实现代码一impo
千家信息网最后更新 2024年10月27日JAVA如何实现连接本地打印机并打印文件
小编给大家分享一下JAVA如何实现连接本地打印机并打印文件,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
实现代码一
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如何实现连接本地打印机并打印文件"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
文件
选择
服务
篇文章
作业
打印机
代码
内容
对话框
属性
格式
类型
学习
定位
对话
不怎么
大部分
实战
技术
教程
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
最新的无线传感器网络技术
盘古网络技术地址
网络安全哪里培训比较好
互联网科技展望
事务型数据库由文件组成
姑苏区智能服务器应用范围
网络安全 网络主权
邮箱服务器掉线
嵌入式的软件开发
邯郸招骋软件开发
嵩山网络安全
无锡电话软件开发
软件开发这个行业前途好吗
易飞erp系统数据库操作
海南企业软件开发市场报价
玉林国家网络安全宣传标语
mac硬件检测软件开发
福建软件开发商
可信赖的数据库高性能
山东少儿编程软件开发
arcgis设置数据库
软件开发老师暑期培训
可能由于服务器维护
物理层软件开发工程师
儿童服务器厂家批发商
bmc端口界面进入服务器
网络版软件如何对接数据库
合肥监狱警察网络安全管理
如何把服务器变成局域网硬盘
光明区网络技术转移包括什么