java根据模板导出pdf文件

java根据模板导出pdf文件

1、使用Adobe Acrobat DC创建表单

在这里插入图片描述
右键点击空白处,新建表单。
在这里插入图片描述
双击表单,重命名(表单名在代码中有用)
在这里插入图片描述

2、引入依赖

<dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.5.1</version>
</dependency>
<dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext-asian</artifactId>
        <version>5.2.0</version>
</dependency>

3、代码

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;


public class pdf {
    // 利用模板生成pdf
    public void pdfout(Map<String, Object> o) {
        // 模板路径
//        String templatePath = "./浙江省自然资源厅受理通知书-样式.pdf";
        String templatePath = "D:\\server\\publish\\qualification\\uploadPath\\template\\杭州市勘测设计研究院有限公司-单位申报信息.pdf";
        // 生成的新文件路径
//        String newPDFPath = "./浙江省自然资源厅受理通知书.pdf";
        String newPDFPath = "D:\\test\\test.pdf";


        try {
            int page = 1;
            FileOutputStream out = new FileOutputStream(newPDFPath);// 输出流

            PdfReader reader = new PdfReader(templatePath);// 读取pdf模板
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            PdfStamper stamper = new PdfStamper(reader, bos);
            AcroFields form = stamper.getAcroFields();
            //文字类的内容处理
            Map<String, String> datemap = (Map<String, String>) o.get("datemap");
            for (String key : datemap.keySet()) {
                String value = datemap.get(key);
                form.setField(key, value);
            }
            //图片类的内容处理
            Map<String, String> imgmap = (Map<String, String>) o.get("imgmap");
            for (String key : imgmap.keySet()) {
                String value = imgmap.get(key);
                String imgpath = value;
                int pageNo = form.getFieldPositions(key).get(0).page;
                Rectangle signRect = form.getFieldPositions(key).get(0).position;
                float x = signRect.getLeft();
                float y = signRect.getBottom();
                //根据路径读取图片
                Image image = Image.getInstance(imgpath);
                //获取图片页面
                PdfContentByte under = stamper.getOverContent(pageNo);
                //图片大小自适应
                image.scaleToFit(signRect.getWidth(), signRect.getHeight());
                //添加图片
                image.setAbsolutePosition(x, y);
                under.addImage(image);
            }


            stamper.setFormFlattening(true);// 如果为false,生成的PDF文件可以编辑,如果为true,生成的PDF文件不可以编辑
            stamper.close();
            Document doc = new Document();
            PdfCopy copy = new PdfCopy(doc, out);
            doc.open();
            int pages = reader.getNumberOfPages();
            int page = 1;
            while(page<pages+1){
                PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()),page);
                copy.addPage(importPage);
                page++;
            }
            doc.close();
            reader.close();
            bos.close();
            stamper.close();
            out.close();
        } catch (IOException e) {
            System.out.println(e);
        } catch (DocumentException e) {
            System.out.println(e);
        }
    }
}


    public static void main(String[] args) {
        Map<String,String> map = new HashMap();
        map.put("name","张三");
        map.put("date","2021/7/19");
        map.put("weather","sunny");

        Map<String,String> map2 = new HashMap();
        map2.put("img","D:/test/test.jpg");

        Map<String,Object> o=new HashMap();
        o.put("datemap",map);
        o.put("imgmap",map2);
        pdfout(o);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值