😔下午刚上班就得到个悲痛得消息,上家工作就干了两个月公司干不下去了,现在老板被抓了。工资还不知道什么时候有着落。/(ㄒoㄒ)/~~忍痛更新一篇博客记录下最近做得其中一个功能。
更加详细的使用方法可以访问我另一篇文章:java使用itext编辑pdf,动态生成pdf文件(从利用Adobe创建pdf模板开始一步步详细介绍)
一、创建pdf模板
使用PDFelement制作pdf模板(数据域的名称对应后面插入的key)
二、导入maven依赖
<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>
三、插入数据和图片到pdf模板
Map<String, Object> 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.pdfString filePath="all.pdf";File file=new File(filePath);file.delete();//要合并的所有pdf的路径List<String> 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";// 生成 pdfPdfUtil.exportTemplateByPdf(savePath, dto.toJson(), path);
pdfUtil
package mon.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 mons.io.IOUtils;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.OutputStream;import .URL;import java.util.Map;@Slf4jpublic 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:\\windows\\fonts\\";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<String, Object> 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) {}}}}
如果觉得《java动态生成pdf文件(使用itext编辑pdf)》对你有帮助,请点赞、收藏,并留下你的观点哦!