您好,登錄后才能下訂單哦!
實現(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(); } } } } }
好了這篇文章就介紹這么多,需要的朋友可以參考一下。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。