对pdf 表单域 或文本框的操作---动态填充PDF 文件内容
前提:需要pdf模板:并且模板内容以pdf 文本框的形式填写
package com.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
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 com.itextpdf.text.pdf.parser.PdfTextExtractor;
public class PdfTest {
@Test
public void testPdf() {
String inputFileName = "certificateModel.pdf";
String outputFileName = "833257_out.pdf";
OutputStream os = null;
PdfStamper ps = null;
PdfReader reader = null;
try {
os = new FileOutputStream(new File(outputFileName));
// 2 读入pdf表单
reader = new PdfReader(inputFileName);
// 3 根据表单生成一个新的pdf
ps = new PdfStamper(reader, os);
// 4 获取pdf表单
AcroFields form = ps.getAcroFields();
// 5给表单添加中文字体 这里采用系统字体。不设置的话,中文可能无法显示
// BaseFont bf = BaseFont.createFont("C:/Windows/Fonts/SIMYOU.TTF",
// BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
// 方法二:使用iTextAsian.jar中的字体
// BaseFont baseFont =
// BaseFont.createFont("STSong-Light",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
BaseFont bf = BaseFont.createFont("d:/user/80004194/desktop/test/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
form.addSubstitutionFont(bf);
// 6查询数据================================================
Map<String, Object> data = new HashMap<String, Object>();
data.put("empName", "金星");
data.put("empNumber", "束带结发看电视");
data.put("empCompany", "顺丰科技");
data.put("empDepartment", "飞行部747");
data.put("empTrainingCenter", "顺丰安全中心");
data.put("empDateOfIssue", "2019-12-3");
data.put("empCertificateNo", "20190100833257");
data.put("empApprovalNo_2019", "ZN-TP-CSS-20180002");
data.put("empTrainingDate_2019", "2019-11-3");
data.put("empCategory_2019", "初训");
data.put("empResult_2019", "合格");
data.put("empSignature_2019", "露西");
//data.put("answer", "张三");
// 7遍历data 给pdf表单表格赋值
for (String key : data.keySet()) {
form.setField(key, data.get(key).toString());
}
ps.setFormFlattening(true);
System.out.println("===============PDF导出成功=============");
} catch (Exception e) {
System.out.println("===============PDF导出失败=============");
e.printStackTrace();
} finally {
try {
ps.close();
reader.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Test
public void readPdf(){
String outputFileName = "2019_833257_out.pdf";
OutputStream os = null;
PdfStamper ps = null;
PdfReader reader = null;
PdfReader modelReader = null;
String inputFileName = "833257.pdf";
String modelUrl="certificateModel.pdf";
String pageContent="";
try {
os = new FileOutputStream(new File(outputFileName));
// 2 读入pdf表单
reader = new PdfReader(inputFileName);
modelReader = new PdfReader(modelUrl);
// 3 根据表单生成一个新的pdf
ps = new PdfStamper(modelReader, os);
// 4 获取pdf表单
AcroFields form = ps.getAcroFields();
BaseFont bf = BaseFont.createFont("d:/user/80004194/desktop/test/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
form.addSubstitutionFont(bf);
//读取pdf数据
int pageNum = reader.getNumberOfPages();
for(int i=1;i<=pageNum;i++){
pageContent = PdfTextExtractor.getTextFromPage(reader, i);
}
Map<String, Object> data = new HashMap<String, Object>();
String[] content=pageContent.split("\n");
//获取所有的培训记录页:
List<String> record=new ArrayList<String>();
String empName="";
String empNumber="";
String empCompany="";
String empDepartment="";
String empTrainingCenter="";
String empDateOfIssue ="";
String empCertificateNo ="";
for(int i =0; i<content.length;i++){
if(content[i].contains("工号")){
String[] empNameAndNumber =content[i].split(" ");
empName = empNameAndNumber[1];
empNumber = empNameAndNumber[3];
}else if(content[i].contains("工作单位")){
String[] empCompanys =content[i].split(" ");
empCompany =empCompanys[1];
}else if(content[i].contains("工作部门")){
String[] empDepartments =content[i].split(" ");
empDepartment = empDepartments[1];
}else if(content[i].contains("培训机构")){
String[] empTrainingCenters =content[i].split(" ");
empTrainingCenter = empTrainingCenters[1];
}else if(content[i].contains("发证日期")){
String[] empDateOfIssues =content[i].split(" ");
empDateOfIssue = empDateOfIssues[1];
}else if(content[i].contains("证书编号")){
String[] empCertificateNos =content[i].split(" ");
empCertificateNo = empCertificateNos[1];
}else if((content[i].contains("初训") || content[i].contains("复训")) && !content[i].contains("有效期")){
record.add(content[i]);
}
}
data.put("empName", empName);
data.put("empNumber", empNumber);
data.put("empCompany", empCompany);
data.put("empDepartment", empDepartment);
data.put("empTrainingCenter", empTrainingCenter);
data.put("empDateOfIssue", empDateOfIssue);
data.put("empCertificateNo", empCertificateNo);
//写入历史详情数据
if(record.size()>0){
for (int i = 0; i < record.size(); i++) {
String[] records=record.get(i).split(" ");
data.put("empApprovalNo_"+(2019+2*i),records[0]);
data.put("empTrainingDate_"+(2019+2*i), records[1]);
data.put("empCategory_"+(2019+2*i),records[2]);
data.put("empResult_"+(2019+2*i), records[3]);
data.put("empSignature_"+(2019+2*i), records[4]);
}
}
//写入新的数据
data.put("empApprovalNo_"+2021, "ZN-GD-TP-0073");
data.put("empTrainingDate_"+2021, "2019-1-21");
data.put("empCategory_"+2021, "复训");
data.put("empResult_"+2021, "优秀");
data.put("empSignature_"+2021, "Jack");
//替换数据
for (String key : data.keySet()) {
form.setField(key, data.get(key).toString());
}
ps.setFormFlattening(true);
}catch (Exception e) {
e.printStackTrace();
}finally {
try {
ps.close();
reader.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
对pdf 表单域 或文本框的操作---动态填充PDF 文件内容的更多相关文章
- HTML中让表单input等文本框为只读不可编辑但可以获取value值的方法;让文本域前面的内容显示在左上角,居中
HTML中让表单input等文本框为只读不可编辑的方法 有时候,我们希望表单中的文本框是只读的,让用户不能修改其中的信息,如使input text的内容,中国两个字不可以修改 有时候,我们希望 ...
- Java 创建、填充PDF表单域
表单域,可以按用途分为多种不同的类型,常见的有文本框.多行文本框.密码框.隐藏域.复选框.单选框和下拉选择框等,目的是用于采集用户的输入或选择的数据.下面的示例中,将分享通过Java编程在PDF中添加 ...
- js中对arry数组的各种操作小结 瀑布流AJAX无刷新加载数据列表--当页面滚动到Id时再继续加载数据 web前端url传递值 js加密解密 HTML中让表单input等文本框为只读不可编辑的方法 js监听用户的键盘敲击事件,兼容各大主流浏览器 HTML特殊字符
js中对arry数组的各种操作小结 最近工作比较轻松,于是就花时间从头到尾的对js进行了详细的学习和复习,在看书的过程中,发现自己平时在做项目的过程中有很多地方想得不过全面,写的不够合理,所以说啊 ...
- PDF表单域(FormField)在HTML显示与提交数据到服务器
1.Adobe Arobat Pro等可以编辑表单域,只有几种控件: 2.展示PDF,可用PdfObject.js,Chrome自带? @{ViewBag.Title = @ViewBag.aaa;} ...
- Jquery学习笔记:操作form表单元素之一(文本框和下拉框)
一.概述 在web页面开发中,经常需要获取和设置表单元素的值(如文本框中的内容),特别是在ajax应用中,更是常态.本文系统的介绍下如何操作. 同操作其它html元素一样,操作的过程差不多. 第一步, ...
- JQuery基础原理 与实例 验证表单 省市联动 文本框判空 单选 复选 判空 下拉判空 确认密码判等
JQuery 基础原理 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...
- HTML中让表单input等文本框为只读不可编辑的方法
有时候,我们希望表单中的文本框是只读的,让用户不能修改其中的信息,如使<input type="text" name="input1" value=&qu ...
- 让表单input等文本框为只读不可编辑的方法-转
有时候,我们希望表单中的文本框是只读的,让用户不能修改其中的信息,如使<input type="text" name="input1" value=&qu ...
- Form表单之复选框checkbox操作
input复选(checkbox): <label>input复选1组:</label> <input type="checkbox" name=&q ...
随机推荐
- CSP-S 模拟53 题解
题解: T1 u: 一看到修改这么多,但询问其实只有一个不难想到差分,但是他这个形状可以说很不规则,于是我们想到分别维护竖着的和斜着的差分,然后最后合并即可. 考场上瞎调了一波系数莫名AC,其实是维护 ...
- node中的http内置模块
Node.js开发的目的就是为了用JavaScript编写Web服务器程序.因为JavaScript实际上已经统治了浏览器端的脚本,其优势就是有世界上数量最多的前端开发人员.如果已经掌握了JavaSc ...
- Linux版本内核及安装后的简单操作命令介绍
一.Linux的版本与内核 1.Linux发行版 Linux发行版= Linux内核+应用程序 Redhat,CentOS,Ubuntu,Suse,红旗,Mint,Fedora CentOS:社区版操 ...
- 【转】mysql基础汇总
mysql基础知识语法汇总整理(二) 原文:https://www.cnblogs.com/cxx8181602/p/9525950.html 连接数据库操作 /*连接mysql*/ mysql - ...
- ArcGIS Python获得一个图层所有线长
import arcpy # Create an empty Geometry object # g = arcpy.Geometry() # Run the CopyFeatures tool, s ...
- Going Deeper with Convolutions阅读摘要
论文链接:Going deeper with convolutions 代码下载: Abstract We propose a deep convolutional neural network ...
- Chrome console不输出内容
设置成info就好了
- java 注解方式 写入数据到Excel文件中
之前有写过一点关于java实现写Excel文件的方法,但是现在看来,那种方式用起来不是太舒服,还很麻烦.所以最近又参考其他,就写了一个新版,用起来不要太爽. 代码不需要解释,惯例直接贴下来: publ ...
- Web实现前后端分离,前后端解耦
一.前言 ”前后端分离“已经成为互联网项目开发的业界标杆,通过Tomcat+Ngnix(也可以中间有个Node.js),有效地进行解耦.并且前后端分离会为以后的大型分布式架构.弹性计算架构.微服务架构 ...
- dede不同栏目调用不同banner图的方法
用顶级栏目ID 方法: <img src="{dede:global.cfg_templets_skin/}/images/{dede:field.typeid function=&q ...