基于IText7 PDF模板填充?

2023-12-10 01:01
文章标签 模板 pdf itext7 填充

本文主要是介绍基于IText7 PDF模板填充?,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

引入依赖

<dependency><groupId>com.itextpdf</groupId><artifactId>itext7-core</artifactId><version>8.0.1</version><type>pom</type>
</dependency>
<dependency><groupId>com.itextpdf</groupId><artifactId>bouncy-castle-adapter</artifactId><version>8.0.1</version>
</dependency>

模板填充工具

import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormCreator;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.*;
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
import com.itextpdf.kernel.pdf.annot.PdfWidgetAnnotation;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.muchenx.util.ImageCompressUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;/*** PDF 模板填充工具类(itext7)* <p>* 支持文本 及 图片 填充*/
@Slf4j
public final class PDFTemplateFillHandler {/*** 待填充的PDF模板文档*/private final transient PdfDocument templateDocument;/*** 填充后PDF文档数据*/private final transient ByteArrayOutputStream destByteOutStream;/*** 字体*/private transient PdfFont font;private PDFTemplateFillHandler(PdfDocument templateDocument, ByteArrayOutputStream destByteOutStream) {this.templateDocument = templateDocument;this.destByteOutStream = destByteOutStream;try {this.font = loadFont();} catch (IOException e) {log.warn("加载指定字体异常:{}", e.getMessage());}}public static PDFTemplateFillHandler loadTemplate(InputStream templateFileStream) {try {ByteArrayOutputStream dest = new ByteArrayOutputStream();PdfDocument pdfDocument = new PdfDocument(new PdfReader(templateFileStream), new PdfWriter(dest));return new PDFTemplateFillHandler(pdfDocument, dest);} catch (IOException e) {throw new RuntimeException(e);}}/*** 填充及返回填充后的数据** @param fillData - 待填充数据* @return - 填充后bytes数据*/public byte[] fill(Map<String, Object> fillData) {try {PdfAcroForm form = PdfAcroForm.getAcroForm(templateDocument, true);fillData.forEach((keyword, value) -> {Optional.ofNullable(form.getField(keyword)).ifPresent(templateFormField -> {if (value instanceof byte[]) {PdfArray pos = templateFormField.getWidgets().get(0).getRectangle();float x = pos.getAsNumber(0).floatValue();float y = pos.getAsNumber(1).floatValue();float width = pos.getAsNumber(2).floatValue() - x;float height = pos.getAsNumber(3).floatValue() - y;Rectangle rectangle = new Rectangle(x, y, width, height);PdfWidgetAnnotation widget = new PdfWidgetAnnotation(rectangle);PdfFormField formField = PdfFormCreator.createFormField(widget, templateDocument);PdfPage annotationPage = findAnnotationPage(keyword);if (annotationPage != null) {doFillFieldImage(annotationPage, formField, (byte[]) value);}} else {templateFormField.setValue(String.valueOf(value));if (font != null) {templateFormField.setFont(templateFormField.getFont());}}form.partialFormFlattening(keyword);});});form.flattenFields();} finally {if (templateDocument != null) {templateDocument.close();}}return destByteOutStream.toByteArray();}/*** 图片填充** @param newPage   - 当前页* @param formField - 表单文本域* @param imgBytes  - 图片文件字节数组*/private void doFillFieldImage(PdfPage newPage, PdfFormField formField, byte[] imgBytes) {Rectangle rtl = formField.getWidgets().get(0).getRectangle().toRectangle(); // 获取表单域的xy坐标PdfCanvas canvas = new PdfCanvas(newPage);ImageData img = ImageDataFactory.create(imgBytes);if (Float.compare(img.getWidth(), rtl.getWidth()) <= 0 && Float.compare(img.getHeight(), rtl.getHeight()) <= 0) {// 不处理canvas.addImageAt(img, rtl.getX(), rtl.getY(), true);} else {// 压缩图片。计算得到图片放缩的最大比例float scale = Math.max(img.getWidth() / rtl.getWidth(), img.getHeight() / rtl.getHeight());int imgWidth = Math.round(img.getWidth() / scale);int imgHeight = Math.round(img.getHeight() / scale);// 压缩图片byte[] compressImgBytes;try {compressImgBytes = ImageCompressUtils.resizeByThumbnails(imgBytes, imgWidth, imgHeight);} catch (IOException e) {throw new RuntimeException(e);}img = ImageDataFactory.create(compressImgBytes);canvas.addImageAt(img, rtl.getX(), rtl.getY(), true);}}/*** 根据表单域关键字查找当前关键字所在页对象(PdfPage)** @param keyword - 关键字* @return - page object*/private PdfPage findAnnotationPage(String keyword) {int pages = templateDocument.getNumberOfPages();for (int index = 1; index <= pages; index++) {PdfPage page = templateDocument.getPage(index);for (PdfAnnotation annotation : page.getAnnotations()) {PdfString title = annotation.getPdfObject().getAsString(PdfName.T);if (title != null && keyword.equals(String.valueOf(title))) {return page;}}}return null;}/*** 获取模板文件表单域关键字位置信息*/private Map<Integer, Map<String, float[]>> getFormKeywordsPos() {int pages = templateDocument.getNumberOfPages();Map<Integer, Map<String, float[]>> maps = new HashMap<>(pages);for (int index = 1; index <= pages; index++) {maps.putIfAbsent(index, new HashMap<>());PdfPage page = templateDocument.getPage(index);// 获取当前页的表单域int finalIndex = index;page.getAnnotations().forEach(anno -> {PdfString title = anno.getTitle();PdfArray rectangle = anno.getRectangle();float x = rectangle.getAsNumber(0).floatValue();float y = rectangle.getAsNumber(1).floatValue();float width = rectangle.getAsNumber(2).floatValue() - x;float height = rectangle.getAsNumber(3).floatValue() - y;maps.get(finalIndex).put(title.getValue(), new float[]{x, y, width, height});});}return maps;}/*** 加载字体*/private PdfFont loadFont() throws IOException {Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:/font/*.ttc");if (resources.length == 0) {return null;}PdfFontFactory.register(resources[0].getURL().getPath(), "SimSun");return PdfFontFactory.createRegisteredFont("SimSun", PdfEncodings.IDENTITY_H,PdfFontFactory.EmbeddingStrategy.PREFER_NOT_EMBEDDED);}
}

填充实例

LocalDate now = LocalDate.now();
// 图片文件
ByteArrayOutputStream catOutStream = new ByteArrayOutputStream();
File img = ResourceUtils.getFile("classpath:cat.png");
InputStream catInStream = Files.newInputStream(img.toPath());
IOUtils.copy(catInStream, catOutStream);
Map<String, Object> data = new HashMap<>();
data.put("username", "喵星人");
data.put("gender", "女");
data.put("nation", "狗");
data.put("school", "狗族大学");
data.put("describe", "千百年来,地球上一直住着一种外星生物,名叫喵星人。它们从遥远的喵星来到地球,化身为猫科动物,分散在世界每个角落。萌萌的外表,加上机智聪明的头脑,轻易就得到人类的宠爱。");
data.put("describe1", "千百年来,地球上一直住着一种外星生物,名叫喵星人。它们从遥远的喵星来到地球,化身为猫科动物,分散在世界每个角落。萌萌的外表,加上机智聪明的头脑,轻易就得到人类的宠爱。");
data.put("content", "千百年来,地球上一直住着一种外星生物,名叫喵星人。它们从遥远的喵星来到地球,化身为猫科动物,分散在世界每个角落。萌萌的外表,加上机智聪明的头脑,轻易就得到人类的宠爱。");
data.put("sign", catOutStream.toByteArray());
data.put("avatar", catOutStream.toByteArray());
data.put("year", String.valueOf(now.getYear()));
data.put("month", String.valueOf(now.getMonthValue()));
data.put("day", String.valueOf(now.getDayOfMonth()));String homePath = FileSystemView.getFileSystemView().getHomeDirectory().getAbsolutePath();
File template = ResourceUtils.getFile("classpath:fill_template.pdf");
byte[] filledDataBytes = PDFTemplateFillHandler.loadTemplate(Files.newInputStream(template.toPath())).fill(data);
IOUtils.write(filledDataBytes, Files.newOutputStream(Paths.get(homePath + "/" + System.nanoTime() + ".pdf")));

基于IText7 的 PDF表单域模板填充

这篇关于基于IText7 PDF模板填充?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/475606

相关文章

Python中图片与PDF识别文本(OCR)的全面指南

《Python中图片与PDF识别文本(OCR)的全面指南》在数据爆炸时代,80%的企业数据以非结构化形式存在,其中PDF和图像是最主要的载体,本文将深入探索Python中OCR技术如何将这些数字纸张转... 目录一、OCR技术核心原理二、python图像识别四大工具库1. Pytesseract - 经典O

Python实现精准提取 PDF中的文本,表格与图片

《Python实现精准提取PDF中的文本,表格与图片》在实际的系统开发中,处理PDF文件不仅限于读取整页文本,还有提取文档中的表格数据,图片或特定区域的内容,下面我们来看看如何使用Python实... 目录安装 python 库提取 PDF 文本内容:获取整页文本与指定区域内容获取页面上的所有文本内容获取

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式

《C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式》Markdown凭借简洁的语法、优良的可读性,以及对版本控制系统的高度兼容性,逐渐成为最受欢迎的文档格式... 目录为什么要将文档转换为 Markdown 格式使用工具将 Word 文档转换为 Markdown(.

Python实现一键PDF转Word(附完整代码及详细步骤)

《Python实现一键PDF转Word(附完整代码及详细步骤)》pdf2docx是一个基于Python的第三方库,专门用于将PDF文件转换为可编辑的Word文档,下面我们就来看看如何通过pdf2doc... 目录引言:为什么需要PDF转Word一、pdf2docx介绍1. pdf2docx 是什么2. by

Python实现pdf电子发票信息提取到excel表格

《Python实现pdf电子发票信息提取到excel表格》这篇文章主要为大家详细介绍了如何使用Python实现pdf电子发票信息提取并保存到excel表格,文中的示例代码讲解详细,感兴趣的小伙伴可以跟... 目录应用场景详细代码步骤总结优化应用场景电子发票信息提取系统主要应用于以下场景:企业财务部门:需

Python对PDF书签进行添加,修改提取和删除操作

《Python对PDF书签进行添加,修改提取和删除操作》PDF书签是PDF文件中的导航工具,通常包含一个标题和一个跳转位置,本教程将详细介绍如何使用Python对PDF文件中的书签进行操作... 目录简介使用工具python 向 PDF 添加书签添加书签添加嵌套书签Python 修改 PDF 书签Pytho

Java如何根据word模板导出数据

《Java如何根据word模板导出数据》这篇文章主要为大家详细介绍了Java如何实现根据word模板导出数据,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... pom.XML文件导入依赖 <dependency> <groupId>cn.afterturn</groupId>

C#实现查找并删除PDF中的空白页面

《C#实现查找并删除PDF中的空白页面》PDF文件中的空白页并不少见,因为它们有可能是作者有意留下的,也有可能是在处理文档时不小心添加的,下面我们来看看如何使用Spire.PDFfor.NET通过C#... 目录安装 Spire.PDF for .NETC# 查找并删除 PDF 文档中的空白页C# 添加与删

Python中Flask模板的使用与高级技巧详解

《Python中Flask模板的使用与高级技巧详解》在Web开发中,直接将HTML代码写在Python文件中会导致诸多问题,Flask内置了Jinja2模板引擎,完美解决了这些问题,下面我们就来看看F... 目录一、模板渲染基础1.1 为什么需要模板引擎1.2 第一个模板渲染示例1.3 模板渲染原理二、模板