SoFunction
Updated on 2025-03-08

JAVA implements the implementation code of connecting to local printers and printing files

Implement code one

import .*;
import ;
import ;
import ;
import ;
import ;
import ;

public class PrintDemo1 {
  public void printPdf(String fileName) {
    //Construct a file selector, default to the current directory    File file = new File(fileName);//Get selected file    //Build a print request attribute set    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
    //Set the print format, because the file type is not determined, select AUTOSENSE here    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
    //Find all available printing services    PrintService printService[] = (flavor, pras);
    //Locate the default printing service    //PrintService defaultService = ();
    // Display the print dialog box    PrintService service = (null, 200, 200, printService, defaultService, flavor, pras);
    if (service != null) {

      try {
        DocPrintJob job = (); // Create a print job        FileInputStream fis; // Construct the file stream to be printed        fis = new FileInputStream(file);
        DocAttributeSet das = new HashDocAttributeSet();
        Doc doc = new SimpleDoc(fis, flavor, das);
        (doc, pras);
      } catch (Exception e) {
        ();
      }
    }
  }

  public static void main(String args[]) {
    PrintDemo1 pic = new PrintDemo1();
    ("F:\\java resource 2\\Docker video tutorial\\Gift 3-Technical practice from Docker to Kubernetes\\01. Why do you need to learn Docker\\");
  }

}

Code Two

package ;

import .*;
import ;
import ;
import ;
import .*;
import ;
import ;

/**
 * Created by Administrator on 2017/9/8 0008.
 */
public class TestController {

  public static void main(String[] args) {
    JFileChooser fileChooser = new JFileChooser(); //Create a print job    int state = (null);
    if(state == fileChooser.APPROVE_OPTION){
      // File file = new File("D:/"); // Get the selected file      File file = ();//Get selected file      //Build a print request attribute set      HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
      //Set the print format, because the type is not determined, so choose autosense      DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
      //Find all available printing services      PrintService printService[] = (flavor, pras);
      //Locate the default printing service      PrintService defaultService = ();
      //Show the print dialog box      PrintService service = (null, 200, 200, printService,
          defaultService, flavor, pras);
      if(service != null){
        try {
          DocPrintJob job = (); //Create a print job          FileInputStream fis = new FileInputStream(file); //Construct the file stream to be printed          DocAttributeSet das = new HashDocAttributeSet();
          Doc doc = new SimpleDoc(fis, flavor, das);
          (doc, pras);
        } catch (Exception e) {
          ();
        }
      }
    }
  }
}

Well, this article will introduce so much. Friends who need it can refer to it.