package com.project.baoxin.config.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
public class PdfUtil {
static boolean result = false;
public static boolean getLicense() {
try {
if (result)
return true;
InputStream strLicense = PdfUtil.class.getClassLoader().getResourceAsStream("License.xml");
strLicense = PdfUtil.class.getClassLoader().getResourceAsStream("License.xml");
com.aspose.cells.License aposeCell = new com.aspose.cells.License();
aposeCell.setLicense(strLicense);
result = true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
return result;
}
public static String fileToPdf(String filePath, String pdfPath) {
// 确保folder是存在的
if (pdfPath.contains(File.separator)) {
String folder = pdfPath.substring(0, pdfPath.lastIndexOf(File.separator));
// FileUtil.createFolder(folder, true);
File dir = new File(folder);
dir.mkdir();
}
File ftmp = new File(pdfPath);
if (!ftmp.exists() || ftmp.length() == 0) {
PdfUtil.excel2pdf(filePath, pdfPath);
}
return “”;
}
/**
* @param excelPath 需要被转换的excel全路径带文件名
* @param pdfPath 转换之后pdf的全路径带文件名
*/
private static void excel2pdf(String excelPath, String pdfPath) {
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return;
}
try {
com.aspose.cells.Workbook wb = new com.aspose.cells.Workbook(excelPath);// 原始excel路径
FileOutputStream fileOS = new FileOutputStream(new File(pdfPath));
wb.save(fileOS, com.aspose.cells.SaveFormat.PDF);
fileOS.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
上面是我读取license用的代码 我的pdf是没有带水印的;
public boolean printPdf(String filePath, PrintService printService) {
try {
PdfViewer viewer = new PdfViewer();
viewer.bindPdf(filePath);
viewer.setAutoResize(true); // Print the file with adjusted size
viewer.setAutoRotate(true); // Print the file with adjusted rotation
viewer.setPrintPageDialog(false); // Do not produce the page number dialog when printing
PdfPrinterSettings printerSettings = new PdfPrinterSettings();
PrintPageSettings pageSettings = new PrintPageSettings();
printerSettings.setPrinterName("RICOH P 201W");
viewer.printDocumentWithSettings(pageSettings, printerSettings);
viewer.close();
return true;
}catch (Exception e) {
log.error("执行pdf打印异常:{}", JSON.toJSONString(e));
return false;
}
}
这是我的打印代码 打印带水印而且很糊