Generate PDF with different encodings using iTextSharp

Let’s assume a following scenario in iTextSharp:
You need to generate a PDF, which would contain text in two or three languages, each of them having its own special characters – for example: German, Italian and Polish (or another Eastern European language).
If you use the standard way of getting fonts, like:

BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, false);

…you will stumble upon a problem, that, using the CP1252 encoding (Latin), the Italian characters with accents are displayed, the German „Umlaute” are displayed, but the Eastern European special characters are not rendered at all in the PDF. On the other hand, using CP1250 encoding (Eastern European), displays the Eastern European characters, but not the Italian special characters with accents, for instance.

First idea would be to use some Unicode or UTF-8 to solve the problem – the „Identity-H” encoding option of iText sounds interesting here. But not with the Helvetica that’s shipped by default.

We could give Arial Unicode a try then, but it is not listed in the enumeration of BaseFont of iText.

Let’s load the font from disk then, and embed it into the document, making sure that all clients will be able to read it properly.

Here is my code snippet that does the trick:

String encoding = BaseFont.IDENTITY_H;
String fontName = "ARIALUNI.TTF";
String fontProjectPath = "MyProject.Namespace.Path." + fontName; // this path contains first the default namespace of your assembly, then the folder path within the project

// loading font from an embedded resource file, in order to avoid any file-system dependencies
using (Stream fontStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(fontProjectPath))
using (MemoryStream ms = new MemoryStream())
BaseFont baseFont = BaseFont.CreateFont(fontName, encoding, BaseFont.EMBEDDED, BaseFont.CACHED, ms.ToArray(), null);

What happens here? It loads the file from the assembly, into memory, and creates the font from the loaded file, embedding it additionally into the PDF document.
Now the document contains both Eastern European and Latin special characters.

Hope this helps.