Martin Dusek
Martin Dusek

Reputation: 1292

iTextSharp - acroform field encoding

I'm trying to fill out a AcroForm's textfield with iTextSharp. The Acroform textfield was created also by iTextSharp by this piece of code:

TextField Field = new TextField(OutputWriter, FieldPos, "MyField");
OutputWriter.AddAnnotation(Field.GetTextField()); // OutputWriter is writing to form.pdf

I fill the form using this code:

PdfReader reader = new PdfReader("form.pdf");
PdfStamper filledOutForm = new PdfStamper(reader, new FileStream("filled_form.pdf", FileMode.Create));

AcroFields form = filledOutForm.AcroFields;
form.SetField("MyField", "some unicode data");

However, when I open filled_form.pdf in Acrobat Reader, unicode characters are not visible unless I edit manually the field (e.g. I append a character by hand to the field).

I also tried to set field's font by:

BaseFont fieldFontRoman = BaseFont.CreateFont(@"C:\Windows\Fonts\times.ttf",
                                BaseFont.IDENTITY_H,
                                BaseFont.EMBEDDED);
form.SetFieldProperty("MyField", "textfont", fieldFontRoman, null);

Then, when I open filled_form.pdf in Acrobat Reader, everything looks fine unless I edit manually the field. After that, non unicode characters disappear (they change into blank spaces). They are here in the field because if I copy whole content of the field by CTRL + C and paste it to notepad, I can see all characters.

Please advise. I would like to see all characters in the field without the need of editing the field manually and of course after editing it manually I would like no characters to disappear.

Thank you

Upvotes: 3

Views: 4892

Answers (1)

VahidN
VahidN

Reputation: 19156

Set the SubstitutionFont after creating the PdfStamper:

stamper.AcroFields.AddSubstitutionFont(myFont.BaseFont);

Upvotes: 6

Related Questions