我正在阅读pdf并更新pdf文本。当我用英语单词替换它似乎很好用,但是当我用阿拉伯语单词替换它却不起作用。
在我的情况下,PdfObject
始终是Indirect
类型,因此在所有情况下dict.get(PdfName.CONTENTS).isArray()
都将是false
public static 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);
String eredeti = "اختبارات";
String arabicWord = new String(eredeti.getBytes());
stream.setData(new String(data).replace("testing", arabicWord ).getBytes("ISO-8859-6"));
}
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
stamper.close();
reader.close();
}