Display Unicode characters in converting Html to Pdf

前端 未结 5 1511
孤城傲影
孤城傲影 2020-11-28 10:33

I am using itextsharp dll to convert HTML to PDF.

The HTML has some Unicode characters like α, β... when I try to convert HTML to PDF, Unicode characters are not sho

5条回答
  •  余生分开走
    2020-11-28 11:32

    private class UnicodeFontFactory : FontFactoryImp
    {
        private BaseFont _baseFont;
    
        public  UnicodeFontFactory()
        {
            string FontPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "arialuni.ttf");
            _baseFont = BaseFont.CreateFont(FontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);                
        }
    
        public override Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color, bool cached)
        {                                
            return new Font(_baseFont, size, style, color);
        }
    }  
    

    //and Code

    FontFactory.FontImp = new UnicodeFontFactory();
    
    string convertedHtml = string.Empty;
    foreach (char c in htmlText)
    {
         if (c < 127)  
               convertedHtml += c;
         else
               convertedHtml += "&#" + (int)c + ";";
    }
    
    List htmlElements = XMLWorkerHelper.ParseToElementList(convertedHtml, null);
    
    // add the IElements to the document
    foreach (IElement htmlElement in htmlElements)
    {                            
          document.Add(htmlElement);
    }
    

提交回复
热议问题