Updating text of a pdf, cannot replace string with Arabic word - itext Java

时光怂恿深爱的人放手 提交于 2019-12-02 15:35:47

问题


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

来源:https://stackoverflow.com/questions/55439445/updating-text-of-a-pdf-cannot-replace-string-with-arabic-word-itext-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!