Click here to Skip to main content
15,867,308 members
Articles / Web Development / ASP.NET
Tip/Trick

Display Vietnamese Characters in PDF Created by iTextSharp

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
15 May 2015CPOL2 min read 13.1K   1
The iTextSharp API is a powerful Open Source tool for creating PDF documents on the fly; it has the ability to generate multilingual PDF but it does not come with the default setting. In order to display Unicode characters such as Vietnamese, please follow our post

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

C#
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() + "/"; // your code goes here
    
    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;
    
    // Add the Paragraph object to the document
    document.Add(title);
    document.Add(author);
    document.Add(tomtat);
    document.Add(summary);
    
    // Close the Document - this saves the document contents to the output stream
    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);
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThank you Pin
ban ghe cafe26-Apr-17 23:19
ban ghe cafe26-Apr-17 23:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.