Blazor Server Application - Chinese font displaying problem on PDF

Cenk 1,036 Reputation points
2022-12-14T06:44:38.01+00:00

Hi,

I am generating a PDF with iText7 as follows but getting Error: iText.IO.Exceptions.IOException: Type of font Arial Unicode MS is not recognized How can I display Chinese characters on PDF?

switch (vendorVM.Warehouse)  
        {  
            case "China Warehouse":  
                FontProgram fontProgram =  
                FontProgramFactory.CreateFont(@"Resources/NotoSansTC-Regular.otf");  
            PdfFont chinese = PdfFontFactory.CreateFont(fontProgram, PdfEncodings.IDENTITY_H);  
                Paragraph delivery = new Paragraph($"Delivery Information: AllSet Electronics.(武汉全易得科技有限公司)")  
                    .SetTextAlignment(TextAlignment.LEFT)  
                    .SetFontSize(14)  
                    .SetFont(chinese);  
                doc.Add(delivery);  

Thank you.

Developer technologies .NET Blazor
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2022-12-15T03:34:24.27+00:00

    Hi @Cenk ,

    Perhaps the issue relates that font file (NotoSansTC-Regular.otf), might be it was corrupted, try to re-download it.

    Besides, you can also store the font file in the wwwroot folder, then access the file via the IWebHostEnvironment and the WebRootPath.

    According to your code, I create a sample (install the iText7 7.2.4 version) and download the font file from here: NotoSansTC-Regular.otf.

    private void PrintPDF()  
    {  
        string[] data = new string[] { "Mahesh Chand", "Mike Gold", "Delivery Information: AllSet Electronics.(武汉全易得科技有限公司)" };  
        var path = Path.Combine(_env.WebRootPath, "files","output.pdf");  
        GeneratePdf(data, path);  
    
    }  
    
    public void GeneratePdf(string[] paragraphs, string destination)  
    {  
        FileInfo file = new FileInfo(destination);  
        file.Delete();  
        var fileStream = file.Create();  
        fileStream.Close();  
        PdfDocument pdfdoc = new PdfDocument(new PdfWriter(file));   
        FontProgram fontProgram = FontProgramFactory.CreateFont(Path.Combine(_env.WebRootPath, "font", "NotoSansCJKsc-Regular.otf"));  
        PdfFont chinese = PdfFontFactory.CreateFont(fontProgram, PdfEncodings.IDENTITY_H);  
       
        using (Document document = new Document(pdfdoc))  
        {  
            foreach (var para in paragraphs)  
            {  
                Paragraph delivery = new Paragraph(para)  
                             .SetTextAlignment(TextAlignment.LEFT)  
                             .SetFontSize(14)  
                             .SetFont(chinese);  
                document.Add(delivery);  
            }  
            document.Close();  
        }  
    }  
    

    Need to add the following reference:

    270745-image.png

    The result as below:

    270710-image1.gif


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.