0

I tried to use the following code to set form field value with iText but it could not show the character ӧ. The test-form-fill.pdf is created by Adobe LifeCycle Form Designer. Please help!

InputStream is = new FileInputStream("doc/test-form-fill.pdf");
OutputStream os = new FileOutputStream("doc/test-form-fill-done.pdf");

PdfReader reader = new PdfReader(is);

PdfStamper stamper = new PdfStamper(reader, os);


AcroFields form = stamper.getAcroFields();

form.setField("field1", "ӧ11111");

stamper.setFormFlattening(true);
stamper.close();
reader.close();


is.close();

os.close();
EricMacau
  • 109
  • 2
  • 5
  • This question has been answered before: http://stackoverflow.com/questions/24305574/arabic-data-disappears-on-form-flattening-in-itext I would like to mark this question as duplicate, but one can only select accepted answers or an answer with an at least one up-vote. – Bruno Lowagie Jul 10 '15 at 12:10

1 Answers1

0

The ӧ character probably doesn't show up because the font that is used for the form field doesn't know how to draw that character. Take a look at the FillFormSpecialChars2 example resulting in form_special_chars.pdf:

enter image description here

You need to define a substitution font like this:

public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader,
            new FileOutputStream(dest));
    AcroFields fields = stamper.getAcroFields();
    BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, false, null, null, false);
    fields.setFieldProperty("Name", "textfont", bf, null);
    fields.setField("Name", "\u04e711111");
    stamper.setFormFlattening(true);
    stamper.close();
}

In my case, FONT refers to FreeSans:

public static final String FONT = "resources/fonts/FreeSans.ttf";

FreeSans knows how to draw an ӧ, but feel free to replace FONT with a path to any other font that supports these characters.

As you can see, I also changed ӧ into \u04e7 which protects your code against encoding problems.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165