1.制作word模板
2.转换成PDF模板
3.使用Adobe Acrobat Pro软件打开PDF,在PDF中选择 表单–>添加或编辑域
4.设置好域名称
5. 新建两个签名备用
6.引入pom文件,编写代码
pom.xml
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/org.springframework.core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.core</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
主程序:
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import org.springframework.util.ResourceUtils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Hello world!
*
*/
public class Pdfmain {
public static void main( String[] args ) {
System.out.println( "调用生成PDF.................." );
Infovo infovo = new Infovo();
infovo.setS1("中国工商银行");
infovo.setS2("中国工商");
infovo.setS3("kinglone");
infovo.setS4("kinglone");
infovo.setS5("202003040001");
infovo.setS6("广东省广州市天河区天河大道1102号");
infovo.setS7("kinglone");
infovo.setS8("020-123654");
infovo.setS9("13200000001");
infovo.setS10("13200000001@qq.com");
infovo.setS11("身份证,421000000000000000");
infovo.setS12("360000000000000000");
infovo.setS13("广东省广州市");
infovo.setS14("4410002");
infovo.setS15("2020");
infovo.setS16("03");
infovo.setS17("04");
String sign1 = "D:/upload/11.jpg";
File DEFAULT_001 = new File(sign1);
String sign2 = "D:/upload/22.jpg";
File DEFAULT_002 = new File(sign2);
Pdfmain pdfmain = new Pdfmain();
pdfmain.getPdfPath(infovo, DEFAULT_001,DEFAULT_002);
}
/**
* pdf生成方法
* @param infovo
* @param DEFAULT_001
* @return
*/
private void getPdfPath(Infovo infovo, File DEFAULT_001,File DEFAULT_002){
String realpath ="";
String[] baoxianTags = { "s1", "s2", "s3", "s4", "s5","s6","s7", "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15", "s16", "s17"};
String[] baoxianValues = {infovo.getS1(),infovo.getS2(),infovo.getS3(),infovo.getS4(),infovo.getS5(),infovo.getS6(),infovo.getS7(),
infovo.getS8(),infovo.getS9(),infovo.getS10(),infovo.getS11(),infovo.getS12(),infovo.getS13(),infovo.getS14(),
infovo.getS15(),infovo.getS16(),infovo.getS17()};
String[] baoxianoptions = { "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text" };
List<FillInFields> fillInFieldsList = getFillInFields(baoxianTags, baoxianValues, baoxianoptions);
try {
realpath= ResourceUtils.getURL("classpath:").getPath() + "fileUpload/";
File file = new File(realpath);
if (!file.exists()) {
file.mkdirs();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//模板地址
String url = "D:/upload/";
String templatePath = url + "123456.pdf";
System.out.println( "模板路径为"+templatePath+".................." );
/* 生成的新文件路径 */
String newPdfPath = realpath + IdGenerator.generate()+".pdf";
String sign1 = "sign1";
String sign2 = "sign2";
PdfReader reader;
FileOutputStream out;
ByteArrayOutputStream bos;
PdfStamper stamper;
int pageNo = 0;
float x = 0;
float x1 = 0;
float y = 0;
float y1 = 0;
int stamppageNo = 0;
float stampx = 0;
float stampx1 = 0;
float stampy = 0;
float stampy1 = 0;
try {
out = new FileOutputStream(newPdfPath);
reader = new PdfReader(templatePath);
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();
ArrayList<BaseFont> fontList = new ArrayList<BaseFont>();
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_CACHED);
fontList.add(bf);
form.setSubstitutionFonts(fontList);
/* 通过域名获取所在页和坐标,左下角为起点 sign1 */
pageNo = form.getFieldPositions(sign1).get(0).page;
Rectangle signRect = form.getFieldPositions(sign1).get(0).position;
x = signRect.getLeft();
x1 = signRect.getRight();
y = signRect.getBottom();
y1 = signRect.getTop();
/* 获取操作的页面 */
PdfContentByte under = stamper.getOverContent(pageNo);
if(null != DEFAULT_001) {
/* 读图片 */
Image image = Image.getInstance(DEFAULT_001.getAbsolutePath());
// 根据域的大小缩放图片
image.scaleToFit(signRect.getWidth(), signRect.getHeight());
// 添加图片
image.setAbsolutePosition(x, y);
under.addImage(image);
}
/* 通过域名获取所在页和坐标,左下角为起点 sign2 */
stamppageNo = form.getFieldPositions(sign2).get(0).page;
Rectangle stampsignRect = form.getFieldPositions(sign2).get(0).position;
stampx = stampsignRect.getLeft();
stampx1 = stampsignRect.getRight();
stampy = stampsignRect.getBottom();
stampy1 = stampsignRect.getTop();
/* 获取操作的页面 */
PdfContentByte stampunder = stamper.getOverContent(stamppageNo);
if(null != DEFAULT_002) {
/* 读图片 */
Image stampimage = Image.getInstance(DEFAULT_002.getAbsolutePath());
// 根据域的大小缩放图片
stampimage.scaleToFit(stampsignRect.getWidth(), stampsignRect.getHeight());
// 添加图片
stampimage.setAbsolutePosition(stampx, stampy);
under.addImage(stampimage);
}
//循环填充字段
Iterator<String> it = form.getFields().keySet().iterator();
while (it.hasNext()) {
String name = it.next().toString();
for (int j = 0; j < fillInFieldsList.size(); j++) {
FillInFields fillinfields = fillInFieldsList.get(j);
if (name.equals(fillinfields.getVarName())) {
form.setField(name, fillinfields.getValue());
break;
}
}
}
// 如果为false那么生成的PDF文件还能编辑,一定要设为true
stamper.setFormFlattening(true);
stamper.close();
Document doc = new Document();
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
int pagecount = reader.getNumberOfPages();
for (int i = 1; i <= pagecount; i++) {
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), i);
copy.addPage(importPage);
}
doc.close();
if(bos != null){
bos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println( "生成PDF文件路径为"+newPdfPath+".................." );
}
public List getFillInFields(String[] varName, String[] values, String[] options) {
List<FillInFields> fillInFieldsList = new ArrayList<FillInFields>();
for (int j = 0; j <= varName.length - 1; j++) {
FillInFields fillInFields = new FillInFields();
fillInFields.setTagTip("");
fillInFields.setType(options[j]);
fillInFields.setValue(values[j]);
fillInFields.setVarName(varName[j]);
fillInFieldsList.add(fillInFields);
}
return fillInFieldsList;
}
}
封装填充的字段
import java.io.Serializable;
public class FillInFields implements Serializable{
/**
*
*/
private static final long serialVersionUID = 6290431644927810113L;
private String tagTip;
private String type;
private String value;
private String varName;
public String getTagTip() {
return tagTip;
}
public void setTagTip(String tagTip) {
this.tagTip = tagTip;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getVarName() {
return varName;
}
public void setVarName(String varName) {
this.varName = varName;
}
}
public class Infovo {
private String s1;
private String s2;
private String s3;
private String s4;
private String s5;
private String s6;
private String s7;
private String s8;
private String s9;
private String s10;
private String s11;
private String s12;
private String s13;
private String s14;
private String s15;
private String s16;
private String s17;
......
}
public class IdGenerator {
/**
* 生成id(uuid+uuid)
* @return uuid
*/
public static String generate() {
String uuid = UUID.randomUUID().toString();
return uuid.replaceAll("\\-", "");
}
}
7.测试