我想要实现的是替换pdf文件中的文本。我有以下代码:
PdfReader reader = new PdfReader("test.pdf");
PdfDictionary dict = reader.getPageN(1);
PdfObject object = dict.getDirectObject(PdfName.CONTENTS);
if (object instanceof PRStream)
{
PRStream stream = (PRStream) object;
byte[] data = PdfReader.getStreamBytes(stream);
System.out.println(new String(data));
stream.setData(new String(data).replace("application", "HELLO WORLD").getBytes());
}
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("test-output.pdf"));
stamper.close();
reader.close();
当我试图打印出来查看数据 (System.out.println(new String(data))) 时,“ application ”显示为“ ap)-4(plica)-3(tion ”,这就是为什么我未能替换文本,任何想法或其他方法可以实现我想要实现的目标?
慕斯709654
相关分类