Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html to pdf some characters are missing (itextsharp)

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 code used to export the pdf is:

 protected void LinkButtonPdf_Click(object sender, EventArgs e)
    {
        Response.ContentType = "application/pdf";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        Response.AddHeader("content-disposition", "attachment;filename=FileName.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        System.IO.StringWriter stringWrite = new StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        GridView1.RenderControl(htmlWrite);
        StringReader reader = new StringReader(textConvert(stringWrite.ToString()));
        Document doc = new Document(PageSize.A4);
        HTMLWorker parser = new HTMLWorker(doc);
        PdfWriter.GetInstance(doc, Response.OutputStream);
        doc.Open();
        parser.Parse(reader);
        doc.Close();
    }
    public static string textConvert(string S)
    {
        if (S == null) { return null; }
        try
        {
            System.Text.Encoding encFrom = System.Text.Encoding.UTF8;
            System.Text.Encoding encTo = System.Text.Encoding.UTF8;
            string str = S;
            Byte[] b = encFrom.GetBytes(str);
            return encTo.GetString(b);
        }
        catch { return null; }
    }

Note: when I want to insert characters into the pdf document, the missing characters are shown in it. I insert the characters with this code:

   BaseFont bffont = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\arial.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        Font fontozel = new Font(bffont, 12, Font.NORMAL, new Color(0, 0, 0));
        doc.Add(new Paragraph("İİııŞŞşşĞĞğğ", fontozel));
like image 785
slayer35 Avatar asked Aug 24 '09 13:08

slayer35


4 Answers

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.

like image 178
slayer35 Avatar answered Nov 20 '22 12:11

slayer35


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);
like image 33
Murat Avatar answered Nov 20 '22 12:11

Murat


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);
                    }
                }
like image 35
ekarakus Avatar answered Nov 20 '22 10:11

ekarakus


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).

like image 2
paracycle Avatar answered Nov 20 '22 10:11

paracycle