这个博客自己现在没时间写,等后面有时间了,自己再写。
这中需求是在实际的项目开发中是会经常遇到的。
下面我们先从简单入手一步一步开始。
1.首先,使用word创建一个6行两列的表格。
点击插入-6行2列的表格,如图所示:
2.将创建好的word文档表格,保存成pdf格式,这里,我使用的是wps,当然office办公软件也是可以的。
3.安装Adobe Acrobat 软件,这是一款非常好用的pdf编辑软件,大家可以百度下载安装和破解,(个人建议:最好全程断网安装)
4.使用Adobe Acrobat软件打开文档1.pdf文件 接下来按照我红色的箭头一步一步执行就OK
当然生成表单的的每个输入框的命名是软件默认给取的,你也可以修改,我这里就先不改了。
5.ctrl+s直接保存就可以了。这样一个正真意义上的pdf模板制作好了就。
6.进行重要的编码工作。
7.首先准备操作pdf文件的的重要jar包,需要在pom.xml文件中进行引入。
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
8.编写相应的处理类
package com.sinosoft.lis.mgubq.zhaoyongqiang; import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException; import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper; /**
* 使用Java操作PDM文件.
*/
public class PDFDemo {
public static void main(String[] args) {
// 模板路径
String templatePath = "D:/pdm/one.pdf";
// 生成的新文件路径
String newPDFPath = "D:/pdm/practise.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[] str = {"123456789", "TOP__ONE", "男", "2000-01-01", "130222111133338888", "河北省张家口市"};
int i = 0;
java.util.Iterator<String> it = form.getFields().keySet().iterator();
while (it.hasNext()) {
String name = it.next().toString();
System.out.println(name);
form.setField(name, str[i++]);
}
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();
System.out.println(e.toString());
} catch (DocumentException e) {
e.printStackTrace();
System.out.println(e.toString());
} } }
运行结果:
这个是form表单中的属性名。你起什么它就叫什么。
9.这是生成的practise.pdf文件
靠,中文乱码,稍等,,,,,,,,,,
10.
https://blog.****.net/top__one/article/details/65442390 这是别人的博客园