JAVA对pdf填充数据

1. 工具:

  1. itextpdf包;
  2. PDFelement免费软件处理pdf表单。功能非常强大,不仅可以在表单编辑中批量插入文本,相比较adobe pdf软件,丝毫不逊色,更重要的是免费使用,并且处理的pdf文档页面干净,不会有任何的讨人厌的水印!

2. jar包:

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

3. 效果图:
在这里插入图片描述
4. 代码:

package org.com.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.*;

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

public class PdfUtil {

	/**
	 * 对pdf进行填充,并输出到指定路径下
	 * @param templatePath 模板绝对路径地址,包括文件名
	 * @param pdfPath      输出的地址,包括文件名
	 * @param stuffData    填充的数据,数据的key与表单的命名和顺序保持一致1
	 * @Return void
	 */
	public static void stuffPdf(String templatePath, String pdfPath, Map<String, String> stuffData) {
		PdfReader reader = null;
		FileOutputStream out = null;
		ByteArrayOutputStream bos = null;
		PdfStamper stamper = null;

		try {
			// 创建字体,使用字体。如果要支持中文,则此处必须添加!!
			BaseFont  baseFont = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H",  BaseFont.NOT_EMBEDDED);

			FileUtil.exist(pdfPath);
			out = new FileOutputStream(pdfPath);
			reader = new PdfReader(templatePath);
			bos = new ByteArrayOutputStream();
			stamper = new PdfStamper(reader, bos);
			AcroFields form = stamper.getAcroFields();
			java.util.Iterator<String> it = form.getFields().keySet().iterator();
			while (it.hasNext()) {
				String name = it.next();
				String data = stuffData.get(name) == null ? "" : stuffData.get(name);
				// 支持中文的设置,不然填充后的pdf中文不显示
				form.addSubstitutionFont(baseFont);
				form.setField(name, data);
			}
			//true代表生成的PDF文件不可编辑
			stamper.setFormFlattening(true);
			stamper.close();
			Document doc = new Document();
			PdfCopy copy = new PdfCopy(doc, out);
			doc.open();
			PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
			copy.addPage(importPage);
			doc.close(); 

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeSource(stamper, reader, bos, out);
		}
	}

// 关闭流
private static void closeSource(PdfStamper stamper,PdfReader reader,ByteArrayOutputStream bos, FileOutputStream out) {
		if (ObjectUtil.isNotEmpty(stamper)) {
			try {
				stamper.close();
			} catch (DocumentException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (ObjectUtil.isNotEmpty(reader)) {
			reader.close();
		}
		if (ObjectUtil.isNotEmpty(out)) {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (ObjectUtil.isNotEmpty(bos)) {
			try {
				bos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

5. 注意事项:

stamper.close(); 
doc.close(); 

stamper和doc的关闭,不能在finally内关闭,不然会报pdf模版有问题异常。

(本文完!转载请说明出处!)

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_函数_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值