Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iTextSharp and special characters (slovak graphemes)

I am having trouble with some special slovak characters (for example č, ň and ť). They are disappearing in the itextsharp generated pdf.

From what I've been able to find, this problem has to do with encoding of my BaseFont. Currently I am using this:

BaseFont.CreateFont(BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.CP1250, BaseFont.NOT_EMBEDDED)

Someone suggested that this should work:

BaseFont.CreateFont(BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED)

But it throws this exception error:

System.ArgumentException was caught
Message='Identity-H' is not a supported encoding name.
Parameter name: name
ParamName=name
Source=mscorlib

Anyone know a possible reason and solution to this?

like image 327
Muleskinner Avatar asked Aug 14 '12 10:08

Muleskinner


1 Answers

The problem is here:

BaseFont.CreateFont(BaseFont.HELVETICA ...

BaseFont.HELVETICA is a standard type 1 font and can't be used for your slovak characters. You need to use a font with the correct glyphs:

string FONT = "c:/windows/fonts/arialbd.ttf";
using (Document document = new Document()) {
  PdfWriter.GetInstance(document, STREAM);
  document.Open();
  BaseFont bf = BaseFont.CreateFont(
    FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED
  );
  document.Add(new Paragraph("č, ň and ť", new Font(bf, 12)));
}
like image 178
kuujinbo Avatar answered Oct 31 '22 00:10

kuujinbo