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


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

    Paragraph preface = new Paragraph();


Please tell me the appropriate solution.