Only English characters visible in PDF reader after adding field's value of existing pdf file using itext

1.8k views Asked by At

I put some values to form fields using following code .

//-------------------------------------------------------

PdfReader reader = new PdfReader(in);

PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(out));

AcroFields form = stamper.getAcroFields();

form.setField("txt_1", "TEST:ทดสอบ");

stamper.close();

reader.close();

//-------------------------------------------------------

the problem is Thai character does not display in the PDF reader until I focus on that field .

** first time I opened output file I got


FieldName: Text:


when I focus on the field the result is correct and after I click on another field, Thai characters is disappear again


FieldName: Text:ทดสอบ


can anyone please help. Thanks

1

There are 1 answers

0
Bruno Lowagie On BEST ANSWER

Please read my answer to Can't get Czech characters while generating a PDF first. It is about Czech characters, but it is also relevant for your question:

  1. you are using a bad programming practice by adding non-Western characters in your code instead of using the UNICODE notation.
  2. you need to create a font that is able to write Thai characters (e.g. arialuni.ttf).

Then read Chapter 8 of my book. More specifically the text that explains figure 8.3 and figure 8.4. Creating text fields containing Unicode characters In figure 8.3, the upper document shows the problem you are experiencing. In this case, we are adding a String with Western text as well as Chinese text as a field value. The Western text is shown, the Chinese text isn't. The second and third window, show what happens when you fill out the field correctly. Filling out text fields containing Unicode characters In figure 8.3, we try adding some Korean text. This fails in the first and third window, but we solved the problem in the second, third and fourth window.

If you do not own a copy of my book, you may benefit from trying the TextFieldFont example that produces all the PDFs shown in the screen shots.

Suppose that you would decide to use MS Arial Unicode, you'd choose the solution where this font is added as substitution font:

AcroFields form = stamper.getAcroFields();
BaseFont unicode =
    BaseFont.createFont("c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
form.addSubstitutionFont(unicode);

There are, of course, other options, but this is one way to solve your problem.