7

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(); 
Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
danarj
  • 1,798
  • 7
  • 26
  • 54

1 Answers1

13

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);
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • this make the pdf file so large it jumps from 2MB to 17MB. – danarj Sep 11 '14 at 20:20
  • 1
    Arial Unicode MS supports 50,000+ glyphs which is why it is so big. The PDF standard (and not just iText) doesn't ship with glyphs for non-English languages so you need to supply a font that does. You can pick any font you want, I just used Arial Unicode MS as an example. – Chris Haas Sep 11 '14 at 20:28
  • is there any way around. – danarj Sep 12 '14 at 09:03
  • Yes, pick a different font. – Chris Haas Sep 12 '14 at 13:13
  • It seams so strange that, on one side, fonts that are available in the operating system need to be embedded into an application in order to be used, and on the other side, that such a (apparently) simple task of using a font, is so complicated (should be read as _not straightforward_) in one of the most used PDF generating framework. What I mean is, thanks! :) – Andrei V Sep 17 '14 at 07:20
  • One of the main reasons that PDF has been so successful for the past 20+ years is that "it just works", and not just _today_ but also _tomorrow_ and _yesterday_, and not just on _Windows_ or _OS X_ but also _Linux_ and _Unix_ and _Amiga_, and not even just on a _computer_ but also on _e-readers_ and _printers_ and _RIPs_. PDF makes almost no assumptions about the underlying PDF renderer. Unfortunately the burden of "it just works" falls on the shoulders of the creator of a PDF but fortunately libraries like iText exist that have shouldered 99% of the burden. – Chris Haas Sep 17 '14 at 13:42