Generating PDF Templates with iText in Java: A Step‑by‑Step Tutorial
This article demonstrates how to create a PDF template by first designing a Word document, converting it to PDF, inserting text, option, and image fields using a PDF editor, and then programmatically filling those fields with Java iText code, including dependency setup and image insertion.
This guide explains how to produce a fillable PDF template starting from a Word document, converting it to PDF, and adding various form fields (text, option, image) using a PDF editor such as Foxit or Adobe Acrobat.
After the visual template is ready, the article shows how to import the PDF into a Java project, add the required iText dependencies, and write code that populates the fields and inserts signature and seal images.
Dependency configuration (Maven):
<!-- pdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.2</version>
</dependency>
<!-- Chinese fonts -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<!-- HTML/XML to PDF -->
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.11</version>
</dependency>Core Java implementation (simplified):
package com.tencent.qcloud.roomservice.webrtc.utils;
import com.alibaba.fastjson.JSONObject;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import java.io.*;
import java.util.Map;
public class PdfTest {
public static void main(String[] args) {
FillTemplate(
"C:\\test\\申请表.pdf",
"C:\\test\\新申请表.pdf",
"杜小七",
"辽宁大连",
"跑步",
"Yes","Yes","Yes",
"C:\\test\\电子签名.png",
"C:\\test\\公章.png"
);
}
public static void FillTemplate(String sourcesPath, String targetPath,
String name, String address, String hobby,
String select_1, String select_2, String select_3,
String signPath, String gongzhangPath) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", name);
jsonObject.put("address", address);
jsonObject.put("hobby", hobby);
jsonObject.put("select_1", select_1);
jsonObject.put("select_2", select_2);
jsonObject.put("select_3", select_3);
PdfReader reader = null;
PdfStamper stamp = null;
try {
reader = new PdfReader(sourcesPath);
stamp = new PdfStamper(reader, new FileOutputStream(targetPath));
AcroFields form = stamp.getAcroFields();
BaseFont song = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
for (Map.Entry
entry : jsonObject.entrySet()) {
String key = entry.getKey();
String value = entry.getValue().toString();
if (key.startsWith("select")) {
form.setField(key, value, true);
} else {
form.setFieldProperty(key, "textfont", song, null);
form.setField(key, value);
}
}
insertImage(form, stamp, "sign", signPath);
insertImage(form, stamp, "gongzhang", gongzhangPath);
stamp.setFormFlattening(true);
} catch (Exception e) {
e.printStackTrace();
} finally {
try { if (stamp != null) stamp.close(); } catch (Exception ignored) {}
try { if (reader != null) reader.close(); } catch (Exception ignored) {}
}
}
public static boolean insertImage(AcroFields form, PdfStamper stamper,
String fieldName, String imagePath) {
try {
int pageNo = form.getFieldPositions(fieldName).get(0).page;
Rectangle rect = form.getFieldPositions(fieldName).get(0).position;
Image img = Image.getInstance(imagePath);
PdfContentByte over = stamper.getOverContent(pageNo);
img.scaleToFit(rect.getWidth(), rect.getHeight());
img.setAbsolutePosition(rect.getLeft(), rect.getBottom());
over.addImage(img);
} catch (Exception e) {
return false;
}
return true;
}
}The article concludes with a screenshot of the test execution and a reminder to follow the public account for more architecture‑related content.
Architect's Guide
Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.