Java实现pdf打印文件
采用Java awt包自带的PrinterJob,和Spire.PDF for Java实现pdf文件打印功能
首先导入Spire.PDF for Java的maven依赖
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<!--Spire.PDF for Java-->
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>5.3.1</version>
</dependency>
</dependencies>
下面直接附代码
package com.ruoyi.common.utils;
import com.spire.pdf.*;
import javax.print.PrintService;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.*;
public class PdfUtil {
public static void main(String[] args) throws Exception {
String fileName = "C:\\Users\\Admin\\Desktop\\testOrder.pdf";
String printerName = "QR-586 LABEL";//打印机名包含字串
PDFprint(new File(fileName), printerName, 412, 582, 0, 0);
}
/**
* 打印面单
*
* @param file pdf文件
* @param printerName 打印机名称
* @param width 打印宽度
* @param height 打印高度
* @param marginLeft 左边距
* @param marginRight 右边距
*/
public static void PDFprint(File file, String printerName, int width, int height, int marginLeft, int marginRight) {
//判断file是否是一个pdf文件
if (!isPdfFile(file)) {
return;
}
//实例化PdfDocument类的对象
PdfDocument pdf = new PdfDocument();
//加载PDF文档
pdf.loadFromFile(file.getAbsolutePath());
//生成打印任务
PrinterJob printJob = PrinterJob.getPrinterJob();
//设置打印任务名称
printJob.setJobName(file.getName());
//查找名称打印机
PrintService printService = getPrintService(printerName);
if (printService != null) {
try {
printJob.setPrintService(printService);
} catch (PrinterException e) {
e.printStackTrace();
}
} else {
System.out.print("打印失败,未找到名称为" + printerName + "的打印机,请检查。");
return;
}
//设置打印参数
PageFormat pageFormat = printJob.defaultPage();
Paper paper = getPaper(width, height, marginLeft, marginRight);
pageFormat.setPaper(paper);
printJob.setPrintable(pdf, pageFormat);
//设置打印份数
printJob.setCopies(1);
try {
printJob.print();
} catch (PrinterException e) {
e.printStackTrace();
}
}
//判断file是否是一个pdf文件
private static boolean isPdfFile(File file) {
if (!file.isFile()) {
//非文件处理
System.out.println("上送文件(" + file.getAbsolutePath() + ")不是一个文件");
return false;
}
if (!file.getName().contains(".pdf")) {
//非pdf文件处理
System.out.println("文件(" + file.getName() + ")不是一个pdf文件");
return false;
}
return true;
}
/**
* 设置打印区域
* @param width 宽度
* @param height 高度
* @param marginLeft 左边距
* @param marginRight 右边距
*/
private static Paper getPaper(int width, int height, int marginLeft, int marginRight) {
Paper paper = new Paper();
int marginTop = 0;
int marginBottom = 0;
paper.setSize(width, height);
// 下面一行代码,解决了打印内容为空的问题
paper.setImageableArea(marginLeft, marginTop, width - (marginLeft + marginRight), height - (marginTop + marginBottom));
return paper;
}
//查找名称打印机
private static PrintService getPrintService(String printerName) {
//获得本台电脑连接的所有打印机
PrintService[] printServices = PrinterJob.lookupPrintServices();
if (printServices == null || printServices.length == 0) {
System.out.print("打印失败,未找到可用打印机,请检查。");
return null;
}
//匹配指定打印机
for (int i = 0; i < printServices.length; i++) {
if (printServices[i].getName().contains(printerName)) {
return printServices[i];
}
}
return null;
}
}
上述代码打印出来,在第一页顶部会出现:EVALUATION WARNING : THE DOCUMENT WAS CREATED WITH SPIRE.PDF FOR JAVA 这么一句水印,可以通过新增空白页后再创作最后删除空白的第一页实现,但是对pdf文件导入方式无效,也可以采用对应免费版本,pdf文件有页数限制,具体查看下述官网。
附Spire.PDF for Java的官方文档:https://www.e-iceblue.cn/pdf_java_print/print-pdf-document-in-java.html