使用C#中的.SetField选项添加PDF格式的特殊文本 [英] Add special text in PDF with .SetField option in C#

查看:328
本文介绍了使用C#中的.SetField选项添加PDF格式的特殊文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我过去曾问过类似的问题,但我已经解决了。这个问题是关于(让我们称之为)C#和PDF(ITextSharp)之间的错误传达。我希望有人可以帮助我。



场景是在PDF编码文件中已经包含了字体,据我所知,字体确实适合于特殊字符。 IEé,ê,ò,ô等等。现在问题出现在我调试代码时。



你看我在过程中可以看到所有角色等已成功传递到pdf



 PdfReader pdfReader =  new  PdfReader( pdfTemplate); 
pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile,FileMode.Create));
AcroFields pdfFormFields = pdfStamper.AcroFields;

pdfFormFields.SetField( PYN,person.PersonFullname);





person.PersonFullname是TRÉVORMÏCHAËLSCHWAÎTZ。这通过代码传递给pdf。但是一旦文件被创建,我只能在pdf中看到:TRVOR MCHAL SCHWATZ。



在PDF编码版本中,我可以看到字体是包括在内。现在我可以看到问题来自于setfield中的名字。



因为这很好用(请注意它是相同的pdf。我只是想不通字体可能是特定单元格中的问题。)



 pdfFormFields.SetField(   BDFC,person.PersonFullname); 





赞我说以上打印正确TRÉVORMÏCHAËLSCHWAÎTZ。



任何人都有任何想法要找什么或者问题可能是什么?

解决方案

我自己解决了!



但对于那些可能偶然发现这个问题的人,我会为你发布答案。



在我更改特定字段的字体之前似乎是编码器,该字段不支持特殊字符。



< pre lang =c#> BaseFont fieldFontRoman =
BaseFont.CreateFont( @ C:\ Windows \Fonts \ timesnr.ttf
BaseFont.IDENTITY_H,
BaseFont.EMBEDDED);

pdfStamper.AcroFields.SetFieldProperty( PYN textfont
fieldFontRoman, null ) ;





所以我刚刚删除了使用特定字体的SetFieldProperty(出于某种原因)



简而言之,您可以使用您知道的特定字体。另一件事是在PDF Coded创建者中,您还必须确保在属性下所有包含/嵌入字体可以处理您将来可能需要的任何所需特殊字符等。



希望我的解决方案对以后任何相同的问题都有所帮助: - )


Hi,

I have asked a similar question in the past but I have solved it. This question is regarding the (Let's call it) "Miscommunication" between C# and PDF(ITextSharp). I am hoping someone can assist me in this.

The scenario is that in the PDF coded file there is already font included and to my understanding the font does cater for the special characters. IE é,ê,ò,ô etc. Now the problem comes in when i debug the code.

You see I can see in the process that all the characters etc are passed successfully into the pdf

PdfReader pdfReader = new PdfReader(pdfTemplate);
pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create));
AcroFields pdfFormFields = pdfStamper.AcroFields;

pdfFormFields.SetField("PYN", person.PersonFullname);



person.PersonFullname is "TRÉVOR MÏCHAËL SCHWAÎTZ". This passes exactly like this to the pdf through the code. But once the file is created and i can only see in the pdf this: "TRVOR MCHAL SCHWATZ".

In the PDF coded version i can see that the font is included. Now what I can see the problem comes in at the name in setfield.

because this works fine (Note it is in the same pdf. I just cannot figure out if the font might be the problem in specific cell.)

pdfFormFields.SetField("BDFC", person.PersonFullname);



Like I said the above prints correctly TRÉVOR MÏCHAËL SCHWAÎTZ.

Anyone have any idee what to look for or what the problem might be?

解决方案

I solved it myself!

But for those that may stumble upon this question I will post the answer for you.

Seems the coder before me change the font for the specific field and that field does not support special characters.

BaseFont fieldFontRoman = 
BaseFont.CreateFont(@"C:\Windows\Fonts\timesnr.ttf", 
                                    BaseFont.IDENTITY_H, 
                                    BaseFont.EMBEDDED); 

            pdfStamper.AcroFields.SetFieldProperty("PYN", "textfont", 
fieldFontRoman, null);



So i just removed the SetFieldProperty that uses the specific font (for some reason)

So in short you can use a specific font that you do know has it. The other thing is in the PDF Coded creator you must also ensure under properties that all the included/embedded fonts can handle any needed special characters that you might need in the future etc.

Hopes my solution will be of help for any future questions that is the same :-)


这篇关于使用C#中的.SetField选项添加PDF格式的特殊文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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