使用iTextSharp完全不显示文本的Unicode到PDF转换

时间:2016-02-25 07:05:45

标签: c# pdf-generation itextsharp unicode-string

我正在使用iTextSharp和HTMLWorker将带有uni代码的HTML(字符串格式)转换为PDF,这里我使用谷歌输入工具键入malayalam,字体是kartika,当转换“കോട്ടയം”时,我没有得到它是

我在这里附上我的代码,请帮助解决此问题的任何解决方案建议任何能满足此要求的API。

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {

            createPDF("<font face='Kartika'> കോട്ടയം </font>");
        }
        catch
        {

        }
    }
    public void createPDF(string html)
    {
        try
        {

            Document doc = new Document(PageSize.LETTER);

            using (FileStream fs = new FileStream(Path.Combine("Test.pdf"), FileMode.Create, FileAccess.Write, FileShare.Read))
            {
                PdfWriter.GetInstance(doc, fs);

                doc.Open();

                //Sample HTML
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append(html);

                //Path to our font
                string KartikaTff = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "Kartika.TTF");
                //Register the font with iTextSharp
                iTextSharp.text.FontFactory.Register(KartikaTff);

                //Create a new stylesheet
                iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet();
                //Set the default body font to our registered font's internal name
                ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.FACE, "Arial Unicode MS");
                //Set the default encoding to support Unicode characters
                ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H);

                //Parse our HTML using the stylesheet created above
                List<IElement> list = HTMLWorker.ParseToList(new StringReader(stringBuilder.ToString()), ST);

                //Loop through each element, don't bother wrapping in P tags
                foreach (var element in list)
                {
                    doc.Add(element);
                }

                doc.Close();
            }






        }
        catch (Exception ex)
        {

        }
    }

0 个答案:

没有答案