1、引入依赖
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.3</version>
</dependency>
2、代码
public void replacePdfWord(String pdfTemplatePath, String savePath) throws IOException, DocumentException {
String filePath = savePath.substring(0, savePath.lastIndexOf('/'));
File file = new File(filePath);
if(!file.exists()) {
file.mkdirs();
}
PdfReader reader = new PdfReader(pdfTemplatePath);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(savePath));
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
ArrayList<BaseFont> fontList = new ArrayList<BaseFont>();
fontList.add(bf);
AcroFields acroFields = stamper.getAcroFields();
acroFields.setSubstitutionFonts(fontList);
acroFields.setField("payTime", "123");
acroFields.setField("price", "456");
acroFields.setField("companyName", "789");
acroFields.setField("orderId", "000");
stamper.setFormFlattening(true);
stamper.close();
}