Html to pdf some characters are missing (itextsharp)

前端 未结 11 1748
不思量自难忘°
不思量自难忘° 2020-12-06 02:34

I want to export gridview to pdf by using the itextsharp library. The problem is that some turkish characters such as İ,ı,Ş,ş etc... are missing in the pdf document. The cod

相关标签:
11条回答
  • 2020-12-06 02:44

    i have finally find a soultution for this problem , by this you can print all turkish character.

    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));
    
    0 讨论(0)
  • 2020-12-06 02:50

    No need to change the source code.

    Try this:

    iTextSharp.text.pdf.BaseFont STF_Helvetica_Turkish = iTextSharp.text.pdf.BaseFont.CreateFont("Helvetica","Cp1254", iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED);    
    
    iTextSharp.text.Font fontNormal = new iTextSharp.text.Font(STF_Helvetica_Turkish, 12, iTextSharp.text.Font.NORMAL);
    
    0 讨论(0)
  • 2020-12-06 02:54

    thank you very much all who posted the samples..

    i use the below solution from codeproject , and there was the turkish char set problems due to font..

    If you use htmlworker you should register font and pass to htmlworker

    http://www.codeproject.com/Articles/260470/PDF-reporting-using-ASP-NET-MVC3

          StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
                    styles.LoadTagStyle("h3", "size", "5");
                    styles.LoadTagStyle("td", "size", ".6");
                    FontFactory.Register("c:\\windows\\fonts\\arial.ttf", "Garamond");   // just give a path of arial.ttf 
                    styles.LoadTagStyle("body", "face", "Garamond");
                    styles.LoadTagStyle("body", "encoding", "Identity-H");
                    styles.LoadTagStyle("body", "size", "12pt");
                    using (var htmlViewReader = new StringReader(htmlText))
                    {
                        using (var htmlWorker = new HTMLWorker(pdfDocument, null, styles))
                        {
                            htmlWorker.Parse(htmlViewReader);
                        }
                    }
    
    0 讨论(0)
  • 2020-12-06 02:55

    Finaly I think I found the solution,I changed itextsharp source code a little in order to show turkish characters.(turkish character code is cp1254)

    I add "public const string CP1254 = "Cp1254";" to [BaseFont.cs] in the source code.

    After that I modify the [FactoryProperties.cs].I changed like this;

    public Font GetFont(ChainedProperties props)
    {
    I don't write the whole code.I changed only code below;
    ------------Default itextsharp code------------------------------------------------------
      if (encoding == null)
                    encoding = BaseFont.WINANSI;
                return fontImp.GetFont(face, encoding, true, size, style, color);
    -------------modified code--------------------------------------------
    
                encoding = BaseFont.CP1254;
                return fontImp.GetFont("C:\\WINDOWS\\Fonts\\arial.ttf", encoding, true, size, style, color);
    }
    

    .After I compile new dll ,and missing characters are shown.

    0 讨论(0)
  • 2020-12-06 02:59

    Don't change the source code of the iTextSharp. Define a new style:

            var styles = new StyleSheet();
            styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.FONTFAMILY, "tahoma");
            styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, "Identity-H");
    

    and then pass it to the HTMLWorker.ParseToList method.

    0 讨论(0)
  • 2020-12-06 03:00

    You can use:

    iTextSharp.text.pdf.BaseFont Vn_Helvetica = iTextSharp.text.pdf.BaseFont.CreateFont(@"C:\Windows\Fonts\arial.ttf", "Identity-H", iTextSharp.text.pdf.BaseFont.EMBEDDED);
    iTextSharp.text.Font fontNormal = new iTextSharp.text.Font(Vn_Helvetica, 12, iTextSharp.text.Font.NORMAL);
    
    0 讨论(0)
提交回复
热议问题