首页 > 其他分享 >根据excel数据和pdf模板批量生成pdf

根据excel数据和pdf模板批量生成pdf

时间:2022-10-13 09:55:53浏览次数:54  
标签:map String excel stamper new pdf data 模板

案例 批量生成合同

1、准备存储数据的excel 和对应的pdf

 

 

 

 

 

 2、利用 Adobe Acrobat DC    制作pdf模板 具体流程如下

  1> 下载Adobe Acrobat DC 并安装 打开准备的pdf

  2> 在右侧工具栏中 找到 准备表单 如图所示

 

 

 

(如果没有准备表单选项,在左上角工具中添加 )

 

   3> 根据需要可以对pdf进行编辑,在适当的位置加上表单控件(本案例添加的为文本框)

 

    4>添加后双击文本框 在弹出的窗口中修改映射的变量名

 

 3、准备完成后进入编码阶段

 public static void main(String[] args) throws Exception {
        FileInputStream file = new FileInputStream("excel文件地址");
        ExcelReader excelReader = EasyExcel.read(file, Excel.class, new ExcelListener()).build();
        ReadSheet readSheet = EasyExcel.readSheet(0).build();
        //读取Excel
        excelReader.read(readSheet);
        //这里千万别忘记关闭,读的时候会创建临时文件,到时磁盘会崩的
        excelReader.finish();
    }
public class ExcelListener implements ReadListener<Excel> {
    
    @Override
    public void invoke(Excel data, AnalysisContext context) {
        System.out.println("解析到一条数据:"+JSON.toJSONString(data) );
        PdfReader reader;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            reader = new PdfReader("pdf模板位置");
            bos = new ByteArrayOutputStream();
            PdfStamper stamper = new PdfStamper(reader, bos);
            AcroFields form = stamper.getAcroFields();
            Map<String, String> map = new HashMap<>();
            map.put("name", data.getName());
            map.put("nameCard",data.getName()+"("+ data.getIdcard()+")");
            map.put("bTime", data.getBTime());
            map.put("eTime", data.getETime());
            fillPdfCellForm(map, form);

            // true代表生成的PDF文件不可编辑
            stamper.setFormFlattening(true);
            stamper.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(new File("生成pdf的地址"));
            bos.writeTo(fos);
        } catch(IOException ioe) {
            // Handle exception here
            ioe.printStackTrace();
        }
    }
    
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        System.out.println("所有数据解析完成!");
    }

    private void fillPdfCellForm(Map<String, String> map, AcroFields form) throws IOException, DocumentException {
        for (Map.Entry entry : map.entrySet()) {
            String key = (String) entry.getKey();
            String value = (String) entry.getValue();
            form.setField(key, value);
        }
    }
}
用到的依赖
<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.0.5</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.10</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>

效果如图

 

 

 

标签:map,String,excel,stamper,new,pdf,data,模板
From: https://www.cnblogs.com/DarkerbeS/p/16787043.html

相关文章