PDF 转图片并插入到 EXCEL 再转PDF

07-21 537阅读

pom.xml 引用

PDF 转图片并插入到 EXCEL 再转PDF
(图片来源网络,侵删)
            com.aspose
            aspose-cells
            21.11
        
        
            com.aspose
            aspose-words
            21.11
        
        
            org.apache.pdfbox
            pdfbox
            2.0.24
        

代码 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 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 getPictureModels(){
        List 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 images = new ArrayList();
            // 创建PDF渲染器
            PDFRenderer pdfRenderer = new PDFRenderer(document);
            for (int i = 0; i  
VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]