IText之pdf表单填充
程序员文章站
2022-06-01 08:30:17
...
maven项目的demo下载地址
文中用到了simsunttc字体自行网上下载。C:/PdfTest/template/为个人创建的文件夹路径。b.pdf存在,b2.pdf不存在,等待项目运行后生成。
项目结构图:
项目代码:
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itext.demo</groupId>
<artifactId>ITextPdfDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ITextPdfDemo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<logback.version>1.1.7</logback.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/font-asian -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>font-asian</artifactId>
<version>7.1.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/io -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>io</artifactId>
<version>7.1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
ITextPdf.java
package com.itextpdf.demo;
import java.io.FileOutputStream;
import com.itextpdf.io.IOException;
import com.itextpdf.io.source.ByteArrayOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
/**
* 采用pdf模板填充技术,pdf上嵌入pdf表单,设置表单名称,进行填充
* @author Administrator
*
*/
public class ITextPdf {
public static void FillForm() throws Exception {
// 模板路径
String templatePath = "C:/PdfTest/template/b.pdf";
// 生成的新文件路径
String newPDFPath = "C:/PdfTest/template/b2.pdf";
PdfReader reader;
FileOutputStream out;
ByteArrayOutputStream bos;
PdfStamper stamper;
try{
out = new FileOutputStream(newPDFPath);// 输出流到新的pdf,没有b2.pdf时会创建
reader = new PdfReader(templatePath);// 读取pdf模板
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();
BaseFont bf = BaseFont.createFont("C:/PdfTest/template/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);//加载字体
//获取form表单的表单名称--》测试看能不能获得pdf文件中的Form表单的名称
java.util.Iterator<String> it = form.getFields().keySet()
.iterator();
while (it.hasNext())
{
String name = it.next().toString();
System.out.println(name);
}
form.setFieldProperty("name", "textfont", bf, null);//设置字体
form.setField("name", "黎明-王厝");
form.setFieldProperty("age", "textfont", bf, null);
form.setField("age", "1");
form.setFieldProperty("class", "textfont", bf, null);
form.setField("class","计算机科学与技术");
stamper.setFormFlattening(true);// 如果为false那么生成的PDF文件还能编辑,一定要设为true
stamper.close();
Document doc = new Document();
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
PdfImportedPage importPage = null;
for(int i = 1; i <= reader.getNumberOfPages(); i++) {
importPage = copy
.getImportedPage(new PdfReader(bos.toByteArray()), i);
copy.addPage(importPage);
}
doc.close();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (DocumentException e)
{
e.printStackTrace();
}
}
}
TestDemo.java
package com.itextpdf.test;
import org.junit.Test;
import com.itextpdf.demo.ITextPdf;
public class TestDemo {
@Test
public void MyTest(){
try {
ITextPdf.FillForm();
} catch (Exception e) {
e.printStackTrace();
}
}
}
b.pdf
有两页,为了测试分页的时候还能不能填充。
未用Adobe Acrobat XI Pro的pdf表单填充前的页面:
用Adobe Acrobat XI Pro进行表单填充:
对生成的表单进行重命名,即点击,右键,重命名
另存为 b.pdf
b2.pdf
上一篇: 背包、队列、栈(附数据类型实现)
下一篇: 背包、队列和栈