iTextSharp - acroform field encoding

安稳与你 提交于 2019-12-12 12:44:18

问题


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


回答1:


Set the SubstitutionFont after creating the PdfStamper:

stamper.AcroFields.AddSubstitutionFont(myFont.BaseFont);


来源:https://stackoverflow.com/questions/10271696/itextsharp-acroform-field-encoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!