iTextSharp - acroform字段编码 [英] iTextSharp - acroform field encoding

查看:173
本文介绍了iTextSharp - acroform字段编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用iTextSharp填写AcroForm的文本字段。 Acroform文本字段由iTextSharp通过这段代码创建:

  TextField Field = new TextField(OutputWriter,FieldPos,MyField ); 
OutputWriter.AddAnnotation(Field.GetTextField()); // OutputWriter正在写入form.pdf

我使用以下代码填写表单:

  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);

但是,当我在Acrobat Reader中打开fill_form.pdf时,unicode字符不可见,除非我手动编辑该字段(例如,我用手附加字符到字段)。



我还尝试通过以下方式设置字段的字体:

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

然后,当我在Acrobat Reader中打开fill_form.pdf时,除非我手动编辑字段。之后,非Unicode字符消失(它们变为空格)。他们在这个领域,因为如果我通过CTRL + C复制字段的整个内容并将其粘贴到记事本,我可以看到所有的字符。



请指教。我想看到字段中的所有字符,而不需要手动编辑字段,当然手动编辑它后,我不希望任何字符消失。



谢谢

解决方案

在创建PdfStamper后设置SubstitutionFont:

  stamper.AcroFields.AddSubstitutionFont(myFont.BaseFont); 


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

解决方案

Set the SubstitutionFont after creating the PdfStamper:

stamper.AcroFields.AddSubstitutionFont(myFont.BaseFont);

这篇关于iTextSharp - acroform字段编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆