Home » Java » How to replace text in Pdf file with Accent letters(Java)

How to replace text in Pdf file with Accent letters(Java)

Posted by: admin July 12, 2018 Leave a comment

Questions:

I’m trying to modify Pdf file using itext library. While replacing some text with normal english letters and numbers there is no issue. But when i insert Accent letters ‘Bɐɑɒ’, it is coming in pdf file like ‘B?’. My Java code is,

public static void addTextToPdf(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 = "Bɐɑɒ123";
        final String s = new String(eredeti.getBytes(), BaseFont.CP1250);

        stream.setData(new String(data).replace("AddressLine1", s).getBytes("UTF-8"));
    }
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    stamper.close();

    Paragraph preface = new Paragraph();
    preface.setAlignment(Element.ALIGN_CENTER);

    reader.close();
}

Please tell me the appropriate solution.

Answers: