因此,我具有此功能,该功能可以替换模板pdf文件中的特定占位符,并将替换后的数据另存为新文件:
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfDictionary dict = reader.getPageN(1);
PdfObject object = dict.getDirectObject(PdfName.CONTENTS);
if (object instanceof PRStream) {
PRStream stream = (PRStream) object;
byte[] data = PdfReader.getStreamBytes(stream);
stream.setData(new String(data).replace("{0}", "one for all").getBytes());
}
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
stamper.close();
reader.close();
if (Desktop.isDesktopSupported()) {
File myFile = new File(dest);
Desktop.getDesktop().open(myFile);
}
}
这是我的src(模板文件)的样子:
创建新文件后,它看起来相同。占位符未更改。如何使用iText替换文本