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) {
//構造一個文件選擇器,默認為當前目錄
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(); // 創(chuàng)建打印作業(yè)
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之技術實戰(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();//獲取選擇的文件
//構建打印請求屬性集
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); //構造待打印的文件流
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);
job.print(doc, pras);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
好了這篇文章就介紹這么多,需要的朋友可以參考一下。
相關文章
SpringCloud-Spring?Boot?Starter使用測試及問題小結
Spring?Boot?Starter?是在?SpringBoot?組件中被提出來的一種概念、簡化了很多煩瑣的配置、通過引入各種?Spring?Boot?Starter?包可以快速搭建出一個項目的腳手架,這篇文章主要介紹了SpringCloud-Spring?Boot?Starter使用測試,需要的朋友可以參考下2022-07-07
Java工程的Resources目錄從基礎到高級應用深入探索
這篇文章主要介紹了Java工程中的resources目錄,從基礎概念到高級應用,涵蓋了如何創(chuàng)建、使用以及資源文件的加載方法,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2025-01-01
Spring?Boot項目中使用?TrueLicense?生成和驗證License的詳細步驟
這篇文章主要介紹了Spring?Boot項目中使用?TrueLicense?生成和驗證License,本文分步驟給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-10-10
Java?Request獲取請求頭數(shù)據(jù)實例詳解
在開發(fā)中我們經(jīng)常需要獲取用戶IP地址,通過地址來實現(xiàn)一些功能,下面這篇文章主要給大家介紹了關于Java中Request獲取請求頭數(shù)據(jù)的相關資料,需要的朋友可以參考下2024-01-01
Java實現(xiàn)網(wǎng)絡文件下載以及下載到指定目錄
在Spring框架中,StreamUtils和FileCopyUtils兩個工具類提供了方便的文件下載功能,它們都屬于org.springframework.util包,可以通過簡單的方法調用實現(xiàn)文件流的復制和下載,這些工具類支持多種參數(shù)傳遞,涵蓋了文件下載的多種場景2024-09-09
說說@ModelAttribute在父類和子類中的執(zhí)行順序
這篇文章主要介紹了@ModelAttribute在父類和子類中的執(zhí)行順序,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
springboot自定義redis-starter的實現(xiàn)
這篇文章主要介紹了springboot自定義redis-starter的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10

