Updating text of a pdf, cannot replace string with

2020-05-07 05:34发布

问题:

I am reading a pdf and updating the text of the pdf. It seems to work fine when I replace it with English word but when I replace it with Arabic word, it doesn't work.

In my case, the PdfObject would always be of type Indirect so dict.get(PdfName.CONTENTS).isArray() would be false in all cases

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();
}