下载包
aspose-word
aspose-cells
maven从本地导入包
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>15.8.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/aspose-words-15.8.0-jdk16.jar</systemPath>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>23.1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/aspose-cells-23.1.jar</systemPath>
</dependency>
maven package 时需要配置springboot是否打包本地文件
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 打包时会将本地jar一起打包 -->
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
破解签名
<License>
<Data>
<Products>
<Product>Aspose.Total for Java</Product>
<Product>Aspose.Words for Java</Product>
</Products>
<EditionType>Enterprise</EditionType>
<SubscriptionExpiry>20991231</SubscriptionExpiry>
<LicenseExpiry>20991231</LicenseExpiry>
<SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
</Data>
<Signature>
sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=
</Signature>
</License>
工具类 AsposeUtil
package com.btxc.financeplatform.common.util;
import cn.hutool.core.date.DateUtil;
import com.aspose.cells.Workbook;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.Range;
import com.aspose.words.SaveFormat;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import lombok.SneakyThrows;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Date;
import java.util.Map;
/**
* @author :
* @date :
* description : Aspose工具类
*/
public class AsposeUtil {
/**
* 加载license 用于破解 不生成水印
*
* @author
*/
@SneakyThrows
private static void getLicense() {
try (InputStream is = AsposeUtil.class.getClassLoader().getResourceAsStream("License.xml")) {
License license = new License();
license.setLicense(is);
}
}
static {
getLicense();
}
/**
* word转pdf
*
* @param wordPath word文件保存的路径
* @param pdfPath 转换后pdf文件保存的路径
* @author
*/
@SneakyThrows
public static void wordToPdf(String wordPath, String pdfPath) {
getLicense();
File file = new File(pdfPath);
try (FileOutputStream os = new FileOutputStream(file)) {
Document doc = new Document(wordPath);
doc.save(os, SaveFormat.PDF);
}
}
/**
* word转pdf
*
* @param ins 输入流
* @param out 输出流
*/
@SneakyThrows
public static void wordToPdf(InputStream ins, OutputStream out) {
getLicense();
Document doc = new Document(ins);
doc.save(out, SaveFormat.PDF);
}
/**
* excel转pdf
*
* @param ins 输入流
* @param out 输出流
*/
@SneakyThrows
public static void excelToPdf(InputStream ins, OutputStream out) {
// getLicense();
Workbook workbook = new Workbook(ins);
workbook.save(out,com.aspose.cells.SaveFormat.PDF);
}
/**
* word 替换占位符
* @param ins
* @param paraMap
* @return
*/
@SneakyThrows
public static Document wordReplace(InputStream ins, Map<String,String> paraMap){
Document doc = new Document(ins);
Range range = doc.getRange();
for (String key : paraMap.keySet()) {
int replace = range.replace(key, paraMap.get(key), false, false);
System.out.println(key + "--->"+ replace);
}
return doc;
}
/**
*
* @Title: itextPDFAddPicture
* @Description: 为pdf加图片(电子合同盖公章)
* @param inputStream 电子合同pdf文件流
* @param map {company:公司公章名称,purpose:公章用途,year:日期}
* @param out 输出流
* @throws Exception 异常参数
*/
public static void itextPDFAddPicture(InputStream inputStream, Map<String, String> map, OutputStream out) throws Exception{
// 1.1 读取模板文件
PdfReader reader = new PdfReader(inputStream);
// 2、创建PdfStamper对象
PdfStamper stamper = new PdfStamper(reader, out);
// 3、设置公章信息
String company = "xxxx公司";// 公司公章名称
String purpose = "认证";// 公章用途
String year = DateUtil.formatDate(new Date());// 日期
// if (.isStrNull(year)) {
// year = new SimpleDateFormat(FilesUtil.getContractTimeFormat()).format(new Date());
// }
// 4、生成公章
BufferedImage bufferedImage = Graphics2DUtil.startGraphics2D(company, purpose, year);// 整个公章图片流
BufferedImage[] imgs = Graphics2DUtil.splitImage(bufferedImage, 1, 2);
BufferedImage leftBufferedImage = imgs[0];// 左边公章图片流
BufferedImage rightBufferedImage = imgs[1];// 右边公章图片流
// 5、读公章图片
Image image = Image.getInstance(imageToBytes(bufferedImage));
Image leftImage = Image.getInstance(imageToBytes(leftBufferedImage));
Image rightImage = Image.getInstance(imageToBytes(rightBufferedImage));
int chunkWidth = 200;// 公章大小,x轴
int chunkHeight = 200;// 公章大小,y轴
// 获取pdf页面的高和宽
Rectangle pageSize = reader.getPageSize(1);
float height = pageSize.getHeight();
float width = pageSize.getWidth();
// 6、为pdf每页加印章
// 设置公章的位置
float xL = width - chunkWidth/2 - 2;
float yL = height/2-chunkHeight/2-25;
float xR = width-chunkHeight/2 + chunkHeight/8 + 4;
float yR = yL;
// 6.1 第一页盖左章
leftImage.scaleToFit(chunkWidth, chunkHeight);// 公章大小
leftImage.setAbsolutePosition(xL, yL);// 公章位置
// 6.2 第二页盖右章
rightImage.scaleToFit(chunkWidth, chunkHeight);// 公章大小
rightImage.setAbsolutePosition(xR, yR);// 公章位置
int pdfPages = reader.getNumberOfPages();// pdf页面页码
// 遍历为每页盖左章或右章
for (int i = 1; i <= pdfPages; i++) {
if (i % 2 == 0) {// 盖右章
stamper.getOverContent(i).addImage(rightImage);
} else {// 盖左章
stamper.getOverContent(i).addImage(leftImage);
}
}
// 6.3 最后一页盖公章
image.scaleToFit(chunkWidth, chunkWidth);
image.setAbsolutePosition(width/2 + 32, height-chunkHeight + 20);
stamper.getOverContent(pdfPages).addImage(image);
// 7、关闭相关流
stamper.close();
out.close();
reader.close();
inputStream.close();
}
@SneakyThrows
public static byte[] imageToBytes(BufferedImage image) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(image, "png", out);
byte[] bytes = out.toByteArray();
return bytes;
}
public static void main(String[] args) throws Exception {
Workbook workbook = new Workbook(new FileInputStream("C:\\Users\\fl0919\\Desktop\\jrmh\\北投供应链金融服务系统功能清单-20221125(1).xlsx"));
FileOutputStream fileOutputStream = new FileOutputStream("D:\\test\\pdf\\北投供应链金融服务系统功能清单.pdf");
workbook.save(fileOutputStream, com.aspose.cells.SaveFormat.PDF);
}
}