pom.xml 引用
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>21.11</version>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>21.11</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.24</version>
</dependency>
代码 PDF 转 图片 其中Excel 转PDF 可以参考 上一篇
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.IoUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.CustomCellWriteHeightConfig;
import com.CustomPictureHandler;
import com.PictureModel;
import com.TtIncomingInspectionReportExcel;
import com.AsposeCellsUtil;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class TestMain {
public static void main(String[] args) {
Long start = new Date().getTime();
try{
FileInputStream fis = new FileInputStream("E:\\workData\\ZYInspectionFillReport.xlsx");
FileOutputStream fos = new FileOutputStream("E:\\workData\\old.xlsx");
TtIncomingInspectionReportExcel reportExcel = new TtIncomingInspectionReportExcel();
ExcelWriter excelWriter = EasyExcel.write(fos)
.registerWriteHandler(new CustomCellWriteHeightConfig())
.withTemplate(fis)
.build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
//开启自动换行,自动换行表示每次写入一条list数据是都会重新生成一行空行,此选项默认是关闭的,需要提前设置为true
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
excelWriter.fill(reportExcel.getClassList(), fillConfig, writeSheet);
List<PictureModel> pictureModelList = getPictureModels();
//设置 COA图片
if(CollectionUtil.isNotEmpty(pictureModelList)){
WriteSheet writeSheetCoa = EasyExcel.writerSheet("COA文件")
.registerWriteHandler(new CustomPictureHandler(pictureModelList, "COA文件"))
.build();
//对象
excelWriter.fill(reportExcel, writeSheetCoa);
}
//对象
excelWriter.fill(reportExcel, writeSheet);
excelWriter.finish();
reportExcel = null;
IoUtil.close(fis);
IoUtil.close(fos);
//创建Workbook 实例并加载示例文档
String copyFilePath = "E:\\workData\\old.pdf";
AsposeCellsUtil.excelToPdf("E:\\workData\\old.xlsx", new FileOutputStream(copyFilePath));
}catch (Throwable t){
t.printStackTrace();
}
Long end = new Date().getTime();
System.out.println(end - start);
}
private static List<PictureModel> getPictureModels(){
List<PictureModel> data = new ArrayList<>();
String pdfPath = "E:\\BaiduNetdiskDownload\\WXWork\\202405080027_戴陆文2的试用期员工转正述职评分(数字产品BG).pdf";
try{
File pdf = new File(pdfPath);
PDDocument document = PDDocument.load(new FileInputStream(pdf));
List<BufferedImage> images = new ArrayList<>();
// 创建PDF渲染器
PDFRenderer pdfRenderer = new PDFRenderer(document);
for (int i = 0; i < document.getNumberOfPages(); i++) {
/**
* 《 72 》 此处设置得越大像素越高,生成得时候也会越久
* DPI 的设置一般根据具体的需求和使用场景来决定。DPI 越高,生成的图片分辨率越大,图像质量也越高,
* 但同时文件大小也会变得更大。通常情况下,如果需要对生成的图片进行放大、裁剪等操作,建议将 DPI 设置得较高,
* 以保证图像质量和细节的清晰度;如果只是需要简单地浏览或共享图片,可以适当降低 DPI 以减小文件大小。在实际开发中,
* 可以根据不同的应用场景进行调整。一般来说,72 DPI 是一个比较常见的默认值,可以作为参考。
*/
BufferedImage image = pdfRenderer.renderImageWithDPI(i, 100, ImageType.RGB);
images.add(image);
}
int row = 0;
for(int i = 0; i < images.size(); i++){
try{
int statRow = row;
int endRow = row+1;
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(images.get(i), "png", os);
data.add(PictureModel.createPictureModel("COA文件", statRow, endRow, 0, 0, os.toByteArray(), "png"));
row = endRow + 1;
}catch (Throwable t){
t.printStackTrace();
}
}
}catch (Throwable t){
t.printStackTrace();
}
return data;
}
}