载入天数...载入时分秒...

使用word生成PDF模版,Java操作表单生成保单、告知书、证明、合同等

版权问题

首先,版权问题,在当前来说不论5.X版本还是7.X版本都是基于AGPL V3许可的。假如您开发的程序仅用于内网,即,使用人员为员工,那么这个协议对于您来说其实是可以规避的,当您开发的程序是大众即面向用户的,个人建议您公司最好购买Custom,或者使用古老的lowagie 2.1.7(使用MPL/LGPL许可),做了一个兼容iText的版本https://github.com/ymasory/iText-4.2.0,此分支只有代码,没有人进行维护,现在还在维护版本在https://github.com/LibrePDF/OpenPDF,当然除了itext还可以尝试其他的库以及其他方式。本文只简单阐述使用下选择框出现x的问题。

方法一

依赖

<dependency>
	<groupId>org.apache.pdfbox</groupId>
	 <artifactId>pdfbox</artifactId>
	 <version>2.0.13</version>
</dependency>
<dependency>
	<groupId>com.itextpdf</groupId>
	<artifactId>itext-asian</artifactId>
	<version>5.2.0</version>
</dependency>
<dependency>
	<groupId>com.itextpdf</groupId>
	<artifactId>itextpdf</artifactId>
	<version>5.5.10</version>
</dependency>

代码参考
PDFUtil工具类

/**
 * 根据模板生成pdf
 *@param pdfName 文件名
 * @param data Map(String,Object)
 * @return 文件保存全路径文件
 */
import cn.hutool.core.date.DateUtil;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import lombok.extern.slf4j.Slf4j;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;

/**
 * @author lin
 */
@Slf4j
public class PDFUtil {
	public static String createPDF(String pdfName, Map<String, Object> data) {
		//静态方法只能使用匿名类
		String file = new Object() {
			public String getPath() {
				return Objects.requireNonNull(this.getClass().getResource("/")).getPath();
			}
		}.getPath();

		System.out.println(file);
		String dateFolder = DateUtil.today();
		String folderPath = file + File.separator + dateFolder;
		//创建上传文件目录
		File folder = new File(folderPath);
		if (!folder.exists()) {
			folder.mkdirs();
		}
		//设置文件名
		String fileName = "_"+pdfName + ".pdf";
		String savePath = folderPath + File.separator +DateUtil.today() + fileName;
		try {
			PdfReader reader = new PdfReader(file+"/TEMP.pdf");
			ByteArrayOutputStream 	bos = new ByteArrayOutputStream();
			PdfStamper  ps = new PdfStamper(reader, bos);
			AcroFields s = ps.getAcroFields();
			//TTC字体一般有多个所以选择正常的加载
			BaseFont bfChinese = BaseFont.createFont(file+"/MSYH.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
			//设置编码格式
			s.addSubstitutionFont(bfChinese);
			//这点可以直接传对象,但是怕变动,所以map好自定义一个值
			for (String key : data.keySet()) {
				if (data.get(key) != null) {
					s.setField(key, data.get(key).toString());
				}
			}
			ps.setFormFlattening(true);
			ps.close();
			FileOutputStream fos = new FileOutputStream(savePath);
			fos.write(bos.toByteArray());
			fos.flush();
			fos.close();
			return savePath;
		} catch (IOException | DocumentException e) {
			log.error("读取文件异常");
			e.printStackTrace();
			return "error";
		}
	}


}
import cn.hutool.core.bean.BeanUtil;
import com.example.demopdf.entry.PdfData;
import com.example.demopdf.utils.PDFUtil;


import java.util.Map;

/**
 * @program: cherrypro
 * @description:
 * @author: lin
 * @create: 2021-06-15 23:56
 */
public class TestDemo {
	public static void main(String[] args) {
		PdfData data = PdfData.builder()
				.text1("XXXXX保险")
				.text2("1111111111111111111")
				.text3("一个小朋友")
				.text4("女")
				.text5("24")
				.text6("1997-01-05")
				.text7("12345619951215741X")
				.text8("185")
				.text9("45")
				.text10(" ")
				.text11(" ")
				.text12(" ")
				.text13("我希望你读很多书,走很远的路。 我希望你爱很多人,
						也被很多人爱。 我希望你走过人山人海,也遍览山河湖海。我希望你看纸质书,送手写的祝福。
						我要你独立坚强温暖明亮, 我要你在这寡淡的世上,深情的活。")
				.text14("木木")
				.text15("2021")
				.text16("07")
				.text17("04")
				.ch1("false")
				.ch4("false")
				.build();
		Map<String, Object> map = BeanUtil.beanToMap(data);
		String s = PDFUtil.createPDF("ayi",map);
		System.out.println(s);
	}
}

模板图片
image
数据域图片
image
生成结果
image


后记:做这个坑在于checkbox,选择时候和理想不一样,代码操作选择就是×,所以反其道行之,后来发现了正确方法。
升级下itextpdf版本,官方已经停止5.0,现在推荐7.0,的确7.0简单,测试之后会放在GitHub。

方法二

pom依赖

 		<dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.16</version>
        </dependency>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.24</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.itextpdf/itext7-core -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext7-core</artifactId>
            <version>7.1.17</version>
            <type>pom</type>
        </dependency>
posted @ 2021-06-15 15:40  旧信  阅读(1500)  评论(3编辑  收藏  举报