溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

JAVA實現(xiàn)連接本地打印機并打印文件的實現(xiàn)代碼

發(fā)布時間:2020-08-31 15:52:25 來源:腳本之家 閱讀:524 作者:piaoyunlive 欄目:編程語言

實現(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();
        }
      }
    }
  }
}

好了這篇文章就介紹這么多,需要的朋友可以參考一下。

向AI問一下細節(jié)

免責聲明:本站發(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)容。

AI