Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fill pdf form with unicode characters

I am trying to insert some unicode charaters (arabic) to PDF form with c# I used iTextSharp library but when I insert the characters and save characters in the PDF file the unicode characters not getting displayed until I double click on the position of the chracters that should be appeared.

string pdfTemplate = @"c:\po.pdf";
string newFile = @"g:\test\completed_fw4.pdf";
PdfReader pdfReader = new PdfReader(pdfTemplate);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create));
AcroFields pdfFormFields = pdfStamper.AcroFields;
pdfFormFields.SetField("position", TextBox1.Text);
pdfStamper.FormFlattening = false;
// close the pdf
pdfStamper.Close(); 
like image 727
danarj Avatar asked Aug 25 '13 20:08

danarj


1 Answers

There's a couple of ways that you can fix this but ultimately you need to specify a font that is capable of rendering your Unicode content.

First, create a BaseFont object pointing to your Unicode font, I'm using Arial Unicode below:

var arialFontPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF");
var arialBaseFont = BaseFont.CreateFont(arialFontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

Then you can either set the font property on each field individually:

pdfFormFields.SetFieldProperty("position", "textfont", arialBaseFont, null);

Or you can add a document-wide substitution font:

pdfFormFields.AddSubstitutionFont(arialBaseFont);
like image 179
Chris Haas Avatar answered Oct 30 '22 14:10

Chris Haas