I am trying to read a document using itext and replace a string in it. But once manipulated all spanish character becomes junk characters. Below is the code for changing the pdf.
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 dataString = new String(data);
dataString = dataString.replace(sourceString, replacementString);
stream.setData(dataString.getBytes("UTF-8"));
}
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
stamper.close();
reader.close();
In the actual pdf there is a string ${address-line-one} which I am replacing to "20th Street"
This works but with this Spanish word which is in the stream
Documentación becomes Documentaci�n
and same for other spanish word.
I also printed the bytes[] in java console, and found that the reading itself doesn't get that character properly.
Any suggestion?