10

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));
Lipis
  • 21,388
  • 20
  • 94
  • 121
slayer35
  • 604
  • 2
  • 8
  • 26

11 Answers11

8

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.

Community
  • 1
  • 1
slayer35
  • 604
  • 2
  • 8
  • 26
  • This perfectly works.Especially when exporting gridview to pdf. Thanks a lot. – bselvan Jan 31 '13 at 09:55
  • Thank you. It did not works at the beginning. In addition to you, I search whole project and change all "BaseFont.WINANSI" --> "BaseFont.CP1254". Then it works perfectly. – VVovoVV Oct 07 '13 at 08:45
7

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);
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Murat
  • 79
  • 1
  • 1
    @Jason Plank this font is posible to assign to html tag body in LoadTagStyle? – Alex Jul 18 '12 at 10:55
  • This sould be answer! var font1 = FontFactory.GetFont(BaseFont.HELVETICA, "Cp1254", BaseFont.NOT_EMBEDDED, 24, Font.BOLD, BaseColor.BLACK); – kaya Nov 23 '15 at 13:43
3

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);
                    }
                }
ekarakus
  • 31
  • 1
2

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

paracycle
  • 7,665
  • 1
  • 30
  • 34
2

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);
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
dungnguyen
  • 21
  • 1
  • @Jason Plank this font is posible to assign to html tag body in LoadTagStyle? – Alex Jul 18 '12 at 10:57
  • @Alex I don't know, I only fixed the formatting on this answer. Unfortunately the author of this answer doesn't seem to be active here anymore. – Jason Plank Jul 19 '12 at 14:21
1

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

Axl
  • 8,272
  • 2
  • 26
  • 18
  • I did what you 've said but nothing changed.I think we have to change the font of htmlworker but don't know how. thanks – slayer35 Aug 25 '09 at 08:55
1
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
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
xoraxbx
  • 11
  • 1
0

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

}
Termininja
  • 6,620
  • 12
  • 48
  • 49
0

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.

VahidN
  • 18,457
  • 8
  • 73
  • 117
0

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));
Vinit Patel
  • 2,408
  • 5
  • 28
  • 53
0

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

Community
  • 1
  • 1
Matt Stuvysant
  • 439
  • 4
  • 6