Search code examples
c#itextpdf-generationitext7cjk

Itext sharp/Itext 5 display Chinese characters


I am using itextsharp version 5.5.13.2

I am trying the solution in How to display chinese characters in pdf file created with iTextSharp but it did not work it is not displaying anything. my document is blank.

FontSelector selector = new FontSelector();
selector.AddFont(FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12));
selector.AddFont(FontFactory.GetFont("MSung-Light", "UniCNS-UCS2-H", BaseFont.NOT_EMBEDDED));
Phrase ph = selector.Process("全澳甲流确诊病例已破");
document.Add(new Paragraph(ph)); 

I am trying to find a solution

I also tried the solution below but it errors b/se the AddToResourceSearch method is not available.

https://social.msdn.microsoft.com/Forums/en-US/0aba545a-c8ba-4389-9439-fcad416ee60d/display-chinesejapanese-characters-in-pdf-in-itextsharp-in-c?forum=csharpgeneral

public iTextSharp.text.Font CreateChineseFont()
        {
            BaseFont.AddToResourceSearch("iTextAsian.dll");
            BaseFont.AddToResourceSearch("iTextAsianCmaps.dll"); //"STSong-Light", "UniGB-UCS2-H", 
            BaseFont baseFT=BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
            iTextSharp.text.Font font = new iTextSharp.text.Font(baseFT);
            return font;
        }


Document document = new Document(PageSize.A4,50, 50, 50, 50);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Chap11.pdf", FileMode.Create));
document.Open();
iTextSharp.text.Font font = CreateChineseFont();
document.Add(new Paragraph("全澳甲流确诊病例已破", font));
document.Close();

Solution

  • First of all I needed to import the following packages via Nugget

    System.Text.Encoding and System.Text.Encoding.CodePages

    using iTextSharp.text.pdf;
    using iTextSharp.text;
    using System.Text;
    
    public static class ItextSharpRenderer
        {
            public static Phrase CreatePhrase(string text)
            {
    
                FontSelector selector = new FontSelector();
                selector.AddFont(FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12));
                selector.AddFont(FontFactory.GetFont("Arial Unicode MS", BaseFont.IDENTITY_H, BaseFont.EMBEDDED));
                return selector.Process(text);
            }
    
    
            private static void InitializeFonts()
            {
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                FontFactory.Register("C:\\Windows\\Fonts\\ARIALUNI.TTF");
    
            }
    
            public static void RenderCJK()
            {
    
                    InitializeFonts();
                    Document pdfDoc = new Document();
                    PdfWriter.GetInstance(pdfDoc, new FileStream("C:\\HelenProjects\\Helen_IronPDFSample\\A_TestPDF.pdf", FileMode.Create));
                    pdfDoc.Open();
    
                    pdfDoc.Add(new Paragraph(CreatePhrase("hello part1 全澳甲流确诊病例已破")));
    
                    pdfDoc.Close();
                    Console.WriteLine("The file was created.");
                    Console.ReadLine();
    
        }
        }
    

    You can copy and put ARIALUNI.TTF in in any folder and use it from there or put it in the project files and refer from there.