1. 程式人生 > 程式設計 >JAVA實現連線本地印表機並列印檔案的實現程式碼

JAVA實現連線本地印表機並列印檔案的實現程式碼

實現程式碼一

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,printService,defaultService,flavor,pras);
    if (service != null) {

      try {
        DocPrintJob job = service.createPrintJob(); // 建立列印作業
        FileInputStream fis; // 構造待列印的檔案流
        fis = new FileInputStream(file);
        DocAttributeSet das = new HashDocAttributeSet();
        Doc doc = new SimpleDoc(fis,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之技術實戰\\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(); //建立列印作業
    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,pras);
      if(service != null){
        try {
          DocPrintJob job = service.createPrintJob(); //建立列印作業
          FileInputStream fis = new FileInputStream(file); //構造待列印的檔案流
          DocAttributeSet das = new HashDocAttributeSet();
          Doc doc = new SimpleDoc(fis,das);
          job.print(doc,pras);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
  }
}

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