JAVA實(shí)現(xiàn)連接本地打印機(jī)并打印文件的實(shí)現(xiàn)代碼
更新時(shí)間:2019年10月20日 22:22:28 作者:piaoyunlive
這篇文章主要介紹了JAVA實(shí)現(xiàn)連接本地打印機(jī)并打印文件的實(shí)現(xiàn)代碼,需要的朋友可以參考下
實(shí)現(xiàn)代碼一
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) {
//構(gòu)造一個(gè)文件選擇器,默認(rèn)為當(dāng)前目錄
File file = new File(fileName);//獲取選擇的文件
//構(gòu)建打印請(qǐng)求屬性集
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
//設(shè)置打印格式,因?yàn)槲创_定文件類型,這里選擇AUTOSENSE
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
//查找所有的可用打印服務(wù)
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
//定位默認(rèn)的打印服務(wù)
//PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
// 顯示打印對(duì)話框
PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras);
if (service != null) {
try {
DocPrintJob job = service.createPrintJob(); // 創(chuàng)建打印作業(yè)
FileInputStream fis; // 構(gòu)造待打印的文件流
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視頻教程\\贈(zèng)送3-從Docker到Kubernetes之技術(shù)實(shí)戰(zhàn)\\01.為什么你需要學(xué)習(xí)Docker\\01.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(); //創(chuàng)建打印作業(yè)
int state = fileChooser.showOpenDialog(null);
if(state == fileChooser.APPROVE_OPTION){
// File file = new File("D:/haha.txt"); //獲取選擇的文件
File file = fileChooser.getSelectedFile();//獲取選擇的文件
//構(gòu)建打印請(qǐng)求屬性集
HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
//設(shè)置打印格式,因?yàn)槲创_定類型,所以選擇autosense
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
//查找所有的可用的打印服務(wù)
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
//定位默認(rèn)的打印服務(wù)
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
//顯示打印對(duì)話框
PrintService service = ServiceUI.printDialog(null, 200, 200, printService,
defaultService, flavor, pras);
if(service != null){
try {
DocPrintJob job = service.createPrintJob(); //創(chuàng)建打印作業(yè)
FileInputStream fis = new FileInputStream(file); //構(gòu)造待打印的文件流
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);
job.print(doc, pras);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
好了這篇文章就介紹這么多,需要的朋友可以參考一下。
相關(guān)文章
SpringBoot使用JDBC獲取相關(guān)的數(shù)據(jù)方法
這篇文章主要介紹了SpringBoot使用JDBC獲取相關(guān)的數(shù)據(jù)方法,JDBC與數(shù)據(jù)庫建立連接、發(fā)送 操作數(shù)據(jù)庫的語句并處理結(jié)果,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-03-03
JFreeChart動(dòng)態(tài)畫折線圖的方法
這篇文章主要為大家詳細(xì)介紹了JFreeChart動(dòng)態(tài)畫折線圖的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
mybatis?實(shí)體類字段大小寫問題?字段獲取不到值的解決
這篇文章主要介紹了mybatis?實(shí)體類字段大小寫問題?字段獲取不到值的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java代碼實(shí)現(xiàn)哈希表(google 公司的上機(jī)題)
這篇文章主要介紹了Java 哈希表詳解(google 公司的上機(jī)題),本文通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
application作用域?qū)崿F(xiàn)用戶登錄擠掉之前登錄用戶代碼
這篇文章主要介紹了application作用域?qū)崿F(xiàn)用戶登錄擠掉之前登錄用戶代碼,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11

