建立企业网站需要什么,天河网站建设公司排名,wplounge wordpress主题,制作网页图片格式项目需求#xff1a;获取列表数据之后直接将数据生成一个pdf。因此需要使用到 itext 对pdf进行直接操作。 环境配置
需要为pdf添加文字域#xff0c;因此需要安装Adobe Acrobat 准备一个空的PDF文件#xff0c;如果有现成的模板更好 依赖配置#xff0c;我们使用itext的7版…项目需求获取列表数据之后直接将数据生成一个pdf。因此需要使用到 itext 对pdf进行直接操作。 环境配置
需要为pdf添加文字域因此需要安装Adobe Acrobat 准备一个空的PDF文件如果有现成的模板更好 依赖配置我们使用itext的7版本 dependencygroupIdcom.itextpdf/groupIdartifactIditext7-core/artifactIdversion7.2.3/versiontypepom/type/dependency 快速使用
使用Adobe Acrobat Pro DC打开空PDF使用 文字域 工具为PDF添加文字域要注意为每个文字域命名。
如果你有现成的模板PDF直接使用识别域可以识别空白区域然后自动生成文字域但是一般都不太准确 如果你的单个数据很多的话可以在属性中设置多行 设置完文字域之后记得保存。 代码实现
SpringBootTest
class StickerApplicationTests {private static final String TEMP_PATH C:\\Users\\An1ong\\Desktop\\Stickers.pdf;//生成PDF的位置private static final String DEST_PATH C:\\Users\\An1ong\\Desktop\\StickersOut.pdf;//本地上字体的路径private static final String FONT_PATH ;Autowiredprivate StickerService stickerService;Testvoid contextLoads() throws IOException {//创建一个新的PDF文件并写入数据PdfReader reader new PdfReader(TEMP_PATH);// 创建一个 PdfWriter 对象以写入新的PDFPdfWriter writer new PdfWriter(DEST_PATH);// 创建一个 PdfDocument 对象PdfDocument pdfDoc new PdfDocument(reader, writer);// 获取 PDF 表单PdfAcroForm form PdfAcroForm.getAcroForm(pdfDoc, false);//获得数据准备填充ListSticker stickerList stickerService.list(10);//文本填充for(int i 0; i stickerList.size();i){Sticker sticker stickerList.get(i);// 生成自定义序号格式为 001、002、003String customId String.format(%03d, i 1);String idFieldName id (i 1);String nameFieldName name (i 1);PdfFormField idField form.getField(idFieldName);if (idField ! null) {idField.setValue(customId);}PdfFormField nameField form.getField(nameFieldName);if (nameField ! null) {nameField.setValue(sticker.getStickerName());}}//消除掉表单域form.flattenFields();//关闭流pdfDoc.close();}
}
行数也不算少但里面的逻辑其实也很简单。这是一个Springboot的单元测试我调用service中方法获取了一个装着对象的列表。
用PdfReader读取你要套写的模板用PdfWriter将数据写入模板。创建出一个PdfDocument对象并将这两个参数传入就可以开始对PDF操作了。
注意这个过程不会直接在原PDF上操作而是生成一个新的PDF进行操作程序结束后原PDF模版还是空白的。 PdfAcroFrom获取PDF表单然后PdfFormField获取其中的文字域最后使用for循环动态的将数据套打在模板上就完成了。
最终会生成一个新的文件 最终效果 之所以要在最后调用form.flattenFields消除掉表单域是因为如果不消除表单域的话就会变成这样。 封装
我们可以把这个在单元测试中的程序封装成工具类重复使用
public class PdfPrintUtil {// private static final String TEMP_PATH C:\\Users\\An1ong\\Desktop\\Stickers.pdf;
//
// private static final String DEST_PATH C:\\Users\\An1ong\\Desktop\\StickersOut.pdf;public static void printPDF(String TEMP_PATH,String DEST_PATH,ListSticker stickerList) throws IOException {//创建一个新的PDF文件并写入数据PdfReader reader new PdfReader(TEMP_PATH);// 创建一个 PdfWriter 对象以写入新的PDFPdfWriter writer new PdfWriter(DEST_PATH);// 创建一个 PdfDocument 对象PdfDocument pdfDoc new PdfDocument(reader, writer);// 获取 PDF 表单PdfAcroForm form PdfAcroForm.getAcroForm(pdfDoc, false);//文本填充for(int i 0; i stickerList.size();i){Sticker sticker stickerList.get(i);// 生成自定义序号格式为 001、002、003String customId String.format(%03d, i 1);String idFieldName id (i 1);String nameFieldName name (i 1);PdfFormField idField form.getField(idFieldName);if (idField ! null) {idField.setValue(customId);}PdfFormField nameField form.getField(nameFieldName);if (nameField ! null) {nameField.setValue(sticker.getStickerName());}}//消除掉表单域form.flattenFields();//关闭流pdfDoc.close();}}