Java动态生成pdf文件(使用itext编辑pdf)

一、创建pdf模板

使用PDFelement制作pdf模板(数据域的名称对应后面插入的key)

二、导入maven依赖


    com.itextpdf
    itextpdf
    5.5.13



    com.itextpdf
    itext-asian
    5.2.0

《2020最新Java基础精讲视频教程和学习路线!》

三、插入数据和图片到pdf模板

Map data;//要插入的数据
        //初始化itext
        //设置编码
        BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
        PdfReader pdfReader=new PdfReader(“pdf模板文件路径”);
        PdfStamper pdfStamper=new PdfStamper(pdfReader, new FileOutputStream(“输出pdf文件路径”));
        AcroFields form = pdfStamper.getAcroFields();
        form.addSubstitutionFont(baseFont);

        //写入数据
        for(String key:data.keySet()){
            String value=data.get(key).toString();
            //key对应模板数据域的名称
            form.setField(key,value);
        }

        //添加图片
        int pageNo = form.getFieldPositions("img").get(0).page;
        Rectangle signRect = form.getFieldPositions("img").get(0).position;
        float x = signRect.getLeft();
        float y = signRect.getBottom();
        Image image = Image.getInstance("图片路径");
        PdfContentByte under = pdfStamper.getOverContent(pageNo);
        //设置图片大小
        image.scaleAbsolute(signRect.getWidth(), signRect.getHeight());
        //设置图片位置
        image.setAbsolutePosition(x, y);
        under.addImage(image);

        //设置不可编辑
        pdfStamper.setFormFlattening(true);
        pdfStamper.close();

PS:合并多个pdf成一个

//先删除之前的all.pdf
        String filePath="all.pdf";
        File file=new File(filePath);
        file.delete();
        //要合并的所有pdf的路径
        List fileList;
        //all.pdf保存路径
        String savepath="all.pdf";
        Document document = null;
        try {
            document = new Document(new PdfReader(fileList.get(0)).getPageSize(1));
            PdfCopy copy = new PdfCopy(document, new FileOutputStream(savepath));
            document.open();
            for (int i = 0; i < fileList.size(); i++) {
                PdfReader reader = new PdfReader(fileList.get(i));
                int n = reader.getNumberOfPages();// 获得总页码
                for (int j = 1; j <= n; j++) {
                    document.newPage();
                    PdfImportedPage page = copy.getImportedPage(reader, j);// 从当前Pdf,获取第j页
                    copy.addPage(page);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } finally {
            if (document != null) {
                document.close();
            }
        }

下面代码是用在项目中使用得截取部分作为参考:
// 保存路径 生成

String savePath =
            GlConfig.getDownloadResourcePath() + "student/" + student.getName() + "ClassHoursProve.pdf";
            // 生成 pdf
    PdfUtil.exportTemplateByPdf(savePath, dto.toJson(), path);

pdfUtil

package com.gl.common.file.util;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.json.JSONObject;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Map;


@Slf4j
public class PdfUtil {

    /**
     * 学时证明模板路径
     */
    public static final URL TEMPLATE_URL = ResourceUtil.getResource("template/ClassHoursProve.pdf");

    /**
     * 导出PDF方法
     *
     * @param savePath 保存服务器路径
     * @param obj      导出的参数
     */
    public static void exportTemplateByPdf(String savePath, JSONObject obj,String filePath) {
        PdfReader reader = null;
        ByteArrayOutputStream bos = null;
        PdfStamper stamper = null;
        OutputStream os = null;
        FileOutputStream out = null;
        File file;
        Document document = null;
        PdfCopy copy = null;
        Document doc = null;

        try {
            /** 实例化文档对象 */
            document = new Document(PageSize.A4, 50, 40, 40, 50);
            /** 创建 PdfWriter 对象 */
            // 打开文档
            document.open();
            /** pdf文档中中文字体的设置,注意一定要添加iTextAsian.jar包 */
            String localFontPath = "c:windowsfonts";
            BaseFont bfChinese =
                    BaseFont.createFont(localFontPath + "simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            FileUtils.deleteFile(savePath);
            file = new File(savePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            out = new FileOutputStream(file);
            reader = new PdfReader(TEMPLATE_URL);
            bos = new ByteArrayOutputStream();
            stamper = new PdfStamper(reader, bos);
            AcroFields form = stamper.getAcroFields();
            // 文字类的内容处理
            form.addSubstitutionFont(bfChinese);
            String vlaues;
            for (Map.Entry entry : obj.entrySet()) {
                vlaues = String.valueOf(entry.getValue());
                if ("photo".equals(entry.getKey()) || "qrcode".equals(entry.getKey())) {
                    try {
                        // 通过域名获取所在页和坐标,左下角为起点
                        int pageNo = form.getFieldPositions(entry.getKey()).get(0).page;
                        Rectangle signRect = form.getFieldPositions(entry.getKey()).get(0).position;
                        // 印章位置
                        Rectangle seal_signRect = form.getFieldPositions("month").get(0).position;
                        float x = signRect.getLeft();
                        float y = signRect.getBottom();
                        // 印章坐标位置
                        float seal_x = seal_signRect.getLeft();
                        float seal_y = seal_signRect.getBottom();
                        // 读图片
                        Image image = Image.getInstance(vlaues);
                        Image seal_image = Image.getInstance(filePath);
                        // 获取操作的页面
                        PdfContentByte under = stamper.getOverContent(pageNo);
                        // 根据域的大小缩放图片
                        image.scaleToFit(signRect.getWidth(), signRect.getHeight());
                        seal_image.scaleToFit(signRect.getWidth(), signRect.getHeight());
                        // 添加图片
                        image.setAbsolutePosition(x, y);
                        seal_image.setAbsolutePosition(seal_x, seal_y);
                        under.addImage(image);
                        under.addImage(seal_image);
                    } catch (Exception e) {
                        log.info(e.getMessage());
                    }

                } else {
                    if ("fileno".equals(entry.getKey())) {
                        form.setFieldProperty(entry.getKey(), "textsize", 50f, null);
                    } else {
                        form.setFieldProperty(entry.getKey(), "textsize", 10f, null);
                    }
                    form.setField(entry.getKey(), vlaues);
                }
            }
            // 如果为false,生成的PDF文件可以编辑,如果为true,生成的PDF文件不可以编辑
            stamper.setFormFlattening(true);
            stamper.close();
            doc = new Document();
            copy = new PdfCopy(doc, out);
            doc.open();
            PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
            copy.addPage(importPage);
            doc.close();
            document.close();
            copy.flush();
            copy.close();

        } catch (Exception e) {
            log.info(e.getMessage());
        } finally {
            try {

                if (stamper != null) {
                    stamper.close();
                    stamper = null;
                }
                if (reader != null) {
                    reader.close();
                    reader = null;
                }
                IOUtils.closeQuietly(os);
                IOUtils.closeQuietly(bos);
                IOUtils.closeQuietly(out);
                if (document != null) {
                    document.close();
                    document = null;
                }
                if (doc != null) {
                    doc.close();
                    doc = null;
                }
                if (copy != null) {
                    copy.flush();
                    copy.close();
                    copy = null;
                }
            } catch (Exception ignored) {
            }
        }
    }
}
链接地址: https://blog.csdn.net/Aurora_____/article/details/111209096

你可能感兴趣的:(后端,java,spring,springboot,程序员)