发布网友 发布时间:2022-04-21 00:19
共2个回答
热心网友 时间:2023-07-29 14:50
接口 PrintService 是 DocPrintJob 的工厂。PrintService 描述了打印机的功能,并可查询它来了解打印机支持的属性
1 package com.tj.print; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 6 import javax.print.Doc; 7 import javax.print.DocFlavor; 8 import javax.print.DocPrintJob; 9 import javax.print.PrintService;10 import javax.print.PrintServiceLookup;11 import javax.print.ServiceUI;12 import javax.print.SimpleDoc;13 import javax.print.attribute.DocAttributeSet;14 import javax.print.attribute.HashDocAttributeSet;15 import javax.print.attribute.HashPrintRequestAttributeSet;16 import javax.swing.JFileChooser;17 18 public class PrintDemo1 {19 public static void main(String[] args) {20 JFileChooser fileChooser = new JFileChooser(); // 创建打印作业21 int state = fileChooser.showOpenDialog(null);22 if (state == fileChooser.APPROVE_OPTION) {23 File file = fileChooser.getSelectedFile(); // 获取选择的文件24 // 构建打印请求属性集25 HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();26 // 设置打印格式,因为未确定类型,所以选择autosense27 DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;28 // 查找所有的可用的打印服务29 PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);30 // 定位默认的打印服务31 PrintService defaultService = PrintServiceLookup32 .lookupDefaultPrintService();33 // 显示打印对话框34 PrintService service = ServiceUI.printDialog(null, 200, 200,35 printService, defaultService, flavor, pras);36 if (service != null) {37 try {38 DocPrintJob job = service.createPrintJob(); // 创建打印作业39 FileInputStream fis = new FileInputStream(file); // 构造待打印的文件流40 DocAttributeSet das = new HashDocAttributeSet();41 Doc doc = new SimpleDoc(fis, flavor, das);42 job.print(doc, pras);43 } catch (Exception e) {44 e.printStackTrace();45 }46 }47 }48 }49 }
热心网友 时间:2023-07-29 14:51
javax.print***
可以 当图形那样打开,挺费劲的
想打印好看、又省事,一般是转pdf等有格式的文档来打印
~~~~~~~~~
~~~~~~~
~~~~~~~~~~~~
~~~~~~~~~~~~~~~~