利用iText操作PDF文件

时间:2021-02-14 05:40:02

使用背景:存在一个PDF模板,此模板预留很多信息进行填写,类似于表单。

利用iText操作PDF文件

使用工具:Adobe Acobat XI Pro(首先在Word里面写好模板,用Word转成PDF,用这玩意打开PDF,以表单形式操作PDF,他会自动识别一部分你预留的填写输入域,其余部分自行添加。同时可以编辑输入域,编辑其字体,大小,字体在输入域的位置等等)。

利用iText操作PDF文件利用iText操作PDF文件



Java部分:

要操作PDF需要先导入iText的jar包,web项目一定要拷贝到lib文件下一份。

java代码:

        String enterpriseCode = enterpriseInfo.getEnterpriseCode();
String randomNum = enterpriseInfo.getRandomNum();
String chineseName = enterpriseInfo.getChineseName();
List tempList = xinList;
int tempListLen = tempList.size();
String agentName = null;
String agentIdentityCode = null;
for (TSealInfo sealInfo : sealInfoList)
{
agentName = sealInfo.getAgentName();
agentIdentityCode = sealInfo.getAgentIdentityCode();
}

Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String sysDate = sdf.format(date);

// 模板路径
String templatePath = getSession().getServletContext()
.getRealPath("/pdfSource/template.pdf");
// 生成的新文件路径
String newPDFPath = getSession().getServletContext()
.getRealPath("/pdfSource/templateL.pdf");
PdfReader reader;
FileOutputStream out;
ByteArrayOutputStream bos;
PdfStamper stamper;

try
{
out = new FileOutputStream(newPDFPath);// 输出流
reader = new PdfReader(templatePath);// 读取pdf模板
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();

String fontPath = getSession().getServletContext()
.getRealPath("/file/pdfSource/simsun.ttc,1");//字体所在路径
BaseFont bf = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);//加载字体
Font font = new Font(bf, 12,Font.NORMAL);

java.util.Iterator<String> it = form.getFields().keySet()
.iterator();
while (it.hasNext())
{
String name = it.next().toString();
System.out.println(name);
}
form.setFieldProperty("enterpriseCode", "textfont", bf, null);//设置字体
form.setField("enterpriseCode", enterpriseCode);
form.setFieldProperty("randomNum", "textfont", bf, null);
form.setField("randomNum", randomNum);
form.setFieldProperty("chineseName", "textfont", bf, null);
form.setField("chineseName", chineseName);
form.setFieldProperty("tempList", "textfont", bf, null);
form.setField("tempList", tempList.toString().substring(1, tempList.toString().length()-1));
form.setFieldProperty("tempListLen", "textfont", bf, null);
form.setField("tempListLen", String.valueOf(tempListLen));
form.setFieldProperty("agentName", "textfont", bf, null);
form.setField("agentName", agentName);
form.setFieldProperty("agentIdentityCode", "textfont", bf, null);
form.setField("agentIdentityCode", agentIdentityCode);
form.setFieldProperty("sysDate", "textfont", bf, null);
form.setField("sysDate", sysDate);

stamper.setFormFlattening(true);// 如果为false那么生成的PDF文件还能编辑,一定要设为true
stamper.close();


Document doc = new Document();

PdfCopy copy = new PdfCopy(doc, out);
doc.open();
PdfImportedPage importPage = copy
.getImportedPage(new PdfReader(bos.toByteArray()), 1);
copy.addPage(importPage);

doc.close();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (DocumentException e)
{
e.printStackTrace();
}

如果需要要在PDF中操汉字,需要下载extrajars,导入字体,尝试未果,放弃。选择导入本地字体包,c:/windows/fonts/ 选择宋体复制到某文件夹下,用代码引用字体并使用,另外一定要在Acrobat中编辑字体格式为宋体,这样子才能正常操作汉字。