How to connect the local printer and print the file with JAVA
Editor to share with you how to connect to the local printer and print files, I believe that most people do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Implementation code one
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) {/ / construct a file selector, default to the current directory File file = new File (fileName) / / get the selected file / / build print request attribute set PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet (); / / set the print format, because the file type is not determined, select AUTOSENSE DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; / / find all available print services PrintService printService [] = PrintServiceLookup.lookupPrintServices (flavor, pras); / / locate the default print service / / PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService () / / display the print dialog box PrintService service = ServiceUI.printDialog (null, 200,200, printService, defaultService, flavor, pras); if (service! = null) {try {DocPrintJob job = service.createPrintJob (); / / create a print job FileInputStream fis; / / construct a file stream to be printed 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 Resources 2\\ Docker Video tutorial 3-Technical practice from Docker to Kubernetes\ 1. Why you need to learn Docker\ 1.pdf ");}}
Code two
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 (); / / create a print job int state = fileChooser.showOpenDialog (null); if (state = = fileChooser.APPROVE_OPTION) {/ / File file = new File ("D:/.txt"); / / get the selected file File file = fileChooser.getSelectedFile () / / get the selected file / / build print request attribute set HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet (); / / set the print format, because the type is not determined, select autosense DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; / / find all available print services PrintService printService [] = PrintServiceLookup.lookupPrintServices (flavor, pras); / / locate the default print service PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService () / / display the print dialog box PrintService service = ServiceUI.printDialog (null, 200,200, printService, defaultService, flavor, pras); if (service! = null) {try {DocPrintJob job = service.createPrintJob (); / / create a print job FileInputStream fis = new FileInputStream (file); / / construct the file stream to print DocAttributeSet das = new HashDocAttributeSet () Doc doc = new SimpleDoc (fis, flavor, das); job.print (doc, pras);} catch (Exception e) {e.printStackTrace ();} these are all the contents of the article "how to connect JAVA to a local printer and print files". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!