Html to pdf some characters are missing (itextsharp)

前端 未结 11 1749
不思量自难忘°
不思量自难忘° 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 03:02

    For Turkish encoding

    CultureInfo ci = new CultureInfo("tr-TR");
    Encoding enc = Encoding.GetEncoding(ci.TextInfo.ANSICodePage);
    

    If you're outputting HTML, try different DOCTYPE tags at the top of the page.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    

    Note if using HTML you may need to HTMLEncode the characters.

    Server.HTMLEncode()

    HttpServerUtility.HtmlEncode()

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

    I strongly suggest not to change itextsharp source code in order to solve this problem. Have a look at my other comment on the subject: https://stackoverflow.com/a/24587745/1138663

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

    I solved the problem. I can provide my the other solution type...

    try
    {
            BaseFont bf = BaseFont.CreateFont("c:\\windows\\fonts\\calibrib.ttf",
                BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            Document document = new Document(PageSize.A4, 25, 25, 30, 30);
            PdfWriter writer = PdfWriter.GetInstance(document, fs);
    
            Font f = new Font(bf, 12f, Font.NORMAL);
            // Open the document to enable you to write to the document
            document.Open();
            // Add a simple and wellknown phrase to the document
            for (int x = 0; x != 100; x++)
            {
                document.Add(new Paragraph("Paragraph - This is a test! ÇçĞğİıÖöŞşÜü",f));
            }
    
            // Close the document
            document.Close();          
    }
    catch(Exception)
    {
    
    }
    
    0 讨论(0)
  • 2020-12-06 03:08

    I am not familiar with the iTextSharp library; however, you seem to be converting the output of your gridview component to a string and reading from that string to construct your PDF document. You also have a strange conversion from UTF-8 to UTF-8 going on.

    From what I can see (given that your GridView is outputting characters correctly) if you are outputting the characters to a string they would be represented as UTF-16 in memory. You probably need to pass this string directly into the PDF library (like how you pass the raw UTF-16 .NET string "İııŞŞşşĞĞğğ" as it is).

    0 讨论(0)
  • 2020-12-06 03:09
    BaseFont bF = BaseFont.CreateFont("c:\\arial.ttf","windows-1254",true);
    Font f = new Font(bF,12f,Font.NORMAL);
    Chunk c = new Chunk();
    c.Font = f;
    c.Append("Turkish characters: ĞÜŞİÖÇ ğüşıöç");
    document.Add(c);
    

    In the first line, you may write these instead of "windows-1254". All works:

    • Cp1254
    • iso-8859-9
    • windows-1254
    0 讨论(0)
提交回复
热议问题