itextSharp - htmlString to pdf Unicode are missing

天涯浪子 提交于 2019-11-28 05:12:13

问题


When I am trying to generate PDF from HTML, some Turkish characters like ĞÜŞİÖÇ ğüşıöç are missing in PDF, I see a space in place of these characters but i want to print that character.

My code is:

 public virtual void print pdf(string html, int id)
    {

    String htmlText = html.ToString();

    Document document = new Document();

    string filePath = HostingEnvironment.MapPath("~/Content/Pdf/");

    PdfWriter.GetInstance(document, new FileStream(filePath + "\\pdf-"+id+".pdf", 
    FileMode.Create));

    document.Open();

    iTextSharp.text.html.simpleparser.HTMLWorker hw =

                     new iTextSharp.text.html.simpleparser.HTMLWorker(document);



    hw.Parse(new StringReader(htmlText));

    document.Close();

    }

please reply i need suggestion ... regards


回答1:


I have finally find a solution for this problem, by this you can print all Turkish characters.

    String htmlText = html.ToString();

    Document document = new Document();

    string filePath = HostingEnvironment.MapPath("~/Content/Pdf/");
    PdfWriter.GetInstance(document, new FileStream(filePath + "\\pdf-"+Name+".pdf", FileMode.Create));
    document.Open();

    iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
    FontFactory.Register(Path.Combine(_webHelper.MapPath("~/App_Data/Pdf/arial.ttf")),  "Garamond");   // just give a path of arial.ttf 
    StyleSheet css = new StyleSheet();
    css.LoadTagStyle("body", "face", "Garamond");
    css.LoadTagStyle("body", "encoding", "Identity-H");
    css.LoadTagStyle("body", "size", "12pt");

    hw.SetStyleSheet(css);

    hw.Parse(new StringReader(htmlText));


来源:https://stackoverflow.com/questions/21423993/itextsharp-htmlstring-to-pdf-unicode-are-missing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!