Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iTextSharp - acroform field encoding

Tags:

encoding

itext

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

like image 351
Martin Dusek Avatar asked May 27 '26 14:05

Martin Dusek


1 Answers

Set the SubstitutionFont after creating the PdfStamper:

stamper.AcroFields.AddSubstitutionFont(myFont.BaseFont);
like image 200
VahidN Avatar answered May 30 '26 07:05

VahidN



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!