zhousq
4 months ago
20 changed files with 1972 additions and 210 deletions
File diff suppressed because it is too large
@ -1,77 +0,0 @@ |
|||
package com.win.print.service; |
|||
public class PrintService { |
|||
// // 传入文件和打印机名称
|
|||
// public void print(File[] files, String printerName) throws PrintException, IOException {
|
|||
// List<InputStream> streams = fileStreams(files);
|
|||
// if (CollUtil.isEmpty(streams)) {
|
|||
// log.warn("缺少打印文件");
|
|||
// throw new NullPointerException("缺少打印文件");
|
|||
// }
|
|||
//
|
|||
// DocFlavor flavor = getDocFlavor();
|
|||
// PrintRequestAttributeSet aset = getPrintRequestAttribute();
|
|||
//
|
|||
// for (InputStream in : streams) {
|
|||
// DocPrintJob docPrintJob = getPrintJob(printerName);
|
|||
// docPrintJob.print(new SimpleDoc(in, flavor, null), aset);
|
|||
// }
|
|||
// }
|
|||
//
|
|||
// private List<InputStream> fileStreams(File[] files) {
|
|||
// if (ArrayUtil.isEmpty(files)) {
|
|||
// log.warn("缺少打印文件");
|
|||
// throw new NullPointerException("缺少打印文件");
|
|||
// }
|
|||
// List<InputStream> ins = new ArrayList<>();
|
|||
// try {
|
|||
// for (File file : files) {
|
|||
// ins.add(new FileInputStream(file));
|
|||
// }
|
|||
// } catch (FileNotFoundException e) {
|
|||
// log.warn("打印文件缺失:{}", e.getMessage());
|
|||
// }
|
|||
// return ins;
|
|||
// }
|
|||
//
|
|||
// private DocFlavor getDocFlavor() {
|
|||
// // 设置打印格式,如果未确定类型,可选择autosense
|
|||
// return DocFlavor.INPUT_STREAM.AUTOSENSE;
|
|||
// }
|
|||
//
|
|||
// private PrintRequestAttributeSet getPrintRequestAttribute() {
|
|||
// // 设置打印参数
|
|||
// PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
|
|||
// aset.add(new Copies(1)); //份数
|
|||
// //aset.add(MediaSize.ISO.A4); //纸张
|
|||
// // aset.add(Finishings.STAPLE);//装订
|
|||
// aset.add(Sides.DUPLEX);//单双面
|
|||
// return aset;
|
|||
// }
|
|||
//
|
|||
// private DocPrintJob getPrintJob(String printerName) {
|
|||
// if (printerName == null) {
|
|||
// log.warn("请选择打印机");
|
|||
// throw new NullPointerException("请选择打印机");
|
|||
// }
|
|||
// //获得本台电脑连接的所有打印机
|
|||
// PrintService[] printServices = PrinterJob.lookupPrintServices();
|
|||
// if (printServices == null || printServices.length == 0) {
|
|||
// log.warn("打印失败,未找到可用打印机,请检查。");
|
|||
// throw new NullPointerException("打印失败,未找到可用打印机,请检查。");
|
|||
// }
|
|||
// //匹配指定打印机
|
|||
// PrintService printService = null;
|
|||
// for (PrintService service : printServices) {
|
|||
// if (printerName.equals(service.getName())) {
|
|||
// printService = service;
|
|||
// break;
|
|||
// }
|
|||
// }
|
|||
// if (printService == null) {
|
|||
// String format = StrUtil.format("打印失败,未找到名称为 [{}] 的打印机,请检查。", printerName);
|
|||
// log.warn(format);
|
|||
// throw new NullPointerException(format);
|
|||
// }
|
|||
// return printService.createPrintJob();
|
|||
// }
|
|||
} |
@ -0,0 +1,95 @@ |
|||
package com.win.print.service; |
|||
|
|||
import cn.hutool.core.collection.CollUtil; |
|||
import cn.hutool.core.util.ArrayUtil; |
|||
import cn.hutool.core.util.StrUtil; |
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
|
|||
import javax.print.*; |
|||
import javax.print.attribute.HashPrintRequestAttributeSet; |
|||
import javax.print.attribute.PrintRequestAttributeSet; |
|||
import javax.print.attribute.standard.Copies; |
|||
import javax.print.attribute.standard.Sides; |
|||
import java.awt.print.PrinterJob; |
|||
import java.io.*; |
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
public class PrintServiceWin { |
|||
private static final Logger log = LoggerFactory.getLogger(PrintTaskProcesser.class); |
|||
// 传入文件和打印机名称
|
|||
public void print(File[] files, String printerName) throws PrintException, IOException { |
|||
List<InputStream> streams = fileStreams(files); |
|||
if (CollUtil.isEmpty(streams)) { |
|||
log.warn("缺少打印文件"); |
|||
throw new NullPointerException("缺少打印文件"); |
|||
} |
|||
|
|||
DocFlavor flavor = getDocFlavor(); |
|||
PrintRequestAttributeSet aset = getPrintRequestAttribute(); |
|||
|
|||
for (InputStream in : streams) { |
|||
DocPrintJob docPrintJob = getPrintJob(printerName); |
|||
docPrintJob.print(new SimpleDoc(in, flavor, null), aset); |
|||
} |
|||
} |
|||
|
|||
private List<InputStream> fileStreams(File[] files) { |
|||
if (ArrayUtil.isEmpty(files)) { |
|||
log.warn("缺少打印文件"); |
|||
throw new NullPointerException("缺少打印文件"); |
|||
} |
|||
List<InputStream> ins = new ArrayList<>(); |
|||
try { |
|||
for (File file : files) { |
|||
ins.add(new FileInputStream(file)); |
|||
} |
|||
} catch (FileNotFoundException e) { |
|||
log.warn("打印文件缺失:{}", e.getMessage()); |
|||
} |
|||
return ins; |
|||
} |
|||
|
|||
private DocFlavor getDocFlavor() { |
|||
// 设置打印格式,如果未确定类型,可选择autosense
|
|||
return DocFlavor.INPUT_STREAM.AUTOSENSE; |
|||
} |
|||
|
|||
private PrintRequestAttributeSet getPrintRequestAttribute() { |
|||
// 设置打印参数
|
|||
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); |
|||
aset.add(new Copies(1)); //份数
|
|||
//aset.add(MediaSize.ISO.A4); //纸张
|
|||
// aset.add(Finishings.STAPLE);//装订
|
|||
aset.add(Sides.DUPLEX);//单双面
|
|||
return aset; |
|||
} |
|||
|
|||
private DocPrintJob getPrintJob(String printerName) { |
|||
if (printerName == null) { |
|||
log.warn("请选择打印机"); |
|||
throw new NullPointerException("请选择打印机"); |
|||
} |
|||
//获得本台电脑连接的所有打印机
|
|||
PrintService[] printServices = PrinterJob.lookupPrintServices(); |
|||
if (printServices == null || printServices.length == 0) { |
|||
log.warn("打印失败,未找到可用打印机,请检查。"); |
|||
throw new NullPointerException("打印失败,未找到可用打印机,请检查。"); |
|||
} |
|||
//匹配指定打印机
|
|||
PrintService printService = null; |
|||
for (PrintService service : printServices) { |
|||
if (printerName.equals(service.getName())) { |
|||
printService = service; |
|||
break; |
|||
} |
|||
} |
|||
if (printService == null) { |
|||
String format = StrUtil.format("打印失败,未找到名称为 [{}] 的打印机,请检查。", printerName); |
|||
log.warn(format); |
|||
throw new NullPointerException(format); |
|||
} |
|||
return printService.createPrintJob(); |
|||
} |
|||
} |
@ -0,0 +1,41 @@ |
|||
package com.win.print.util; |
|||
|
|||
|
|||
import org.apache.pdfbox.pdmodel.PDDocument; |
|||
import org.apache.pdfbox.printing.PDFPageable; |
|||
import org.apache.pdfbox.printing.PDFPrintable; |
|||
|
|||
import javax.print.PrintService; |
|||
import javax.print.attribute.PrintRequestAttributeSet; |
|||
|
|||
import java.awt.print.*; |
|||
import java.io.File; |
|||
import java.io.IOException; |
|||
|
|||
public class PdfPrintUtil { |
|||
public void print(File pdfFile, PrintService printService, PrintRequestAttributeSet attrs) throws IOException { |
|||
// 加载PDF文档
|
|||
PDDocument document = PDDocument.load(pdfFile); |
|||
try { |
|||
// 创建一个打印任务
|
|||
PrinterJob job = PrinterJob.getPrinterJob(); |
|||
PDFPrintable pdfPrintable = new PDFPrintable(document); |
|||
// 查找并设置打印机
|
|||
if (printService != null) { |
|||
job.setPrintService(printService); |
|||
} |
|||
job.setPrintable(pdfPrintable); |
|||
job.setJobName("标签打印"); |
|||
job.setCopies(1); |
|||
// 执行打印
|
|||
job.print(attrs); |
|||
} catch (PrinterException printerException) { |
|||
throw new RuntimeException(printerException); |
|||
} finally { |
|||
// 关闭PDF文档
|
|||
document.close(); |
|||
} |
|||
|
|||
} |
|||
|
|||
} |
Loading…
Reference in new issue