JAVA實現(xiàn)連接本地打印機并打印文件的實現(xiàn)代碼
更新時間:2019年10月20日 22:22:28 作者:piaoyunlive
這篇文章主要介紹了JAVA實現(xiàn)連接本地打印機并打印文件的實現(xiàn)代碼,需要的朋友可以參考下
實現(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)造一個文件選擇器,默認為當前目錄
File file = new File(fileName);//獲取選擇的文件
//構(gòu)建打印請求屬性集
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(); // 創(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視頻教程\\贈送3-從Docker到Kubernetes之技術(shù)實戰(zhàn)\\01.為什么你需要學習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)建打印請求屬性集
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(); //創(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é)果,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03
Java代碼實現(xiàn)哈希表(google 公司的上機題)
這篇文章主要介紹了Java 哈希表詳解(google 公司的上機題),本文通過圖文實例相結(jié)合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
application作用域?qū)崿F(xiàn)用戶登錄擠掉之前登錄用戶代碼
這篇文章主要介紹了application作用域?qū)崿F(xiàn)用戶登錄擠掉之前登錄用戶代碼,具有一定參考價值,需要的朋友可以了解下。2017-11-11

