Introduction
I am new to iTextSharp and it took me a few days to create a dynamic PDF that can display both English and Vietnamese characters There are not many writings out there that would give me a complete coverage on how this is done, so I figured it will be beneficial to folks who are still struggling like I was that I summarize all the important steps in one post for easier reference on this tricky issue. Even though my case only deals with Simplified Chinese characters, the same technique is applicable to any double-byte Unicode character set, I believe.
There is plenty of documentation about how to use the iTextSharp API to create dynamic PDF, so here I will only focus on what is generally missing in those web posts – getting Vietnamese or Asian characters in general to display correctly alongside with English ones. You can also visit my page to see the complete result at Thai Nguyen University.
You will notice that at the end of each article, there is a link to view article in PDF format.
Getting the API
Get the latest version of iTextSharp.dll from here. Once downloaded and unzipped, there is only one file itextsharp.dll; I added a reference to it in my C# project and that was it. I also downloaded iTextAsian.dll and iTextAsianCmaps.dll (from the extras link on the same download page for iTextSharp.dll) and added references to them, but it turned out I did not need them at all for what I wanted to accomplish, so later I removed them from the project.
Getting the Font File
In my project, I use futura.ttf file. One can simply search this file on Google and place it in the Fonts folder in the web root application.
Using the Code
List<ScientificReport> lstScienreport = repository.ScientificReports.ToList();
foreach (var item in lstScienreport)
{
string file = string.Format("{0}.pdf", item.ScientificReportID.ToString());
string path = "~/Documents/ScientificReport/" +
item.ScientificReportID.ToString() + "/";
bool exists = System.IO.Directory.Exists(Server.MapPath(path));
if (!exists)
System.IO.Directory.CreateDirectory(Server.MapPath(path));
var document = new Document(iTextSharp.text.PageSize.A4, 50, 50, 25, 25);
BaseFont arialCustomer = BaseFont.CreateFont(Server.MapPath("~/font/Futura.ttf"),
BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
var output = new FileStream(Server.MapPath(path) + file, FileMode.Create);
var writer = PdfWriter.GetInstance(document, output);
document.Open();
document.AddAuthor(item.AuthorName);
document.AddKeywords("Journal of Science, Thai Nguyen University,
Dai hoc Thai Nguyen, www.tnu.edu.vn");
document.AddTitle(item.ReportName);
document.AddSubject("Journal of Science, Thai Nguyen University,
Dai hoc Thai Nguyen, www.tnu.edu.vn");
var title = new Paragraph(item.ReportName.ToUpper(), new Font(arialCustomer, 14, Font.BOLD));
title.Alignment = Element.ALIGN_CENTER;
var author = new Paragraph(item.AuthorName, new Font(arialCustomer));
author.Alignment = Element.ALIGN_RIGHT;
author.Font.IsItalic();
var tomtat = new Paragraph("TÓM T?T: ", new Font(arialCustomer, 12, Font.BOLD));
var summary = new Paragraph(Server.HtmlDecode
(StripHTML(item.ReportContent)), new Font(arialCustomer));
summary.Alignment = Element.ALIGN_JUSTIFIED;
document.Add(title);
document.Add(author);
document.Add(tomtat);
document.Add(summary);
document.Close();
item.ReportContent += "<p><a href=http://qlkh.tnu.edu.vn/Documents/ScientificReport/" +
item.ScientificReportID + "/" + file + "> T?i file " +
item.ReportName + " t?i dây</a></p>";
repository.SaveScientificReport(item);
TempData["message"] = string.Format("{0} has been saved", item.ScientificReportID);
}