views:

3710

answers:

6

Hi,I want to export gridview to pdf by using itextsharp library.But problem is some turkish characters such as İ,ı,Ş,ş etc... are missing in the pdf document.Code used to export 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:But when I want to insert characters into the pdf document ,missing characters are shown in the pdf document ,I insert characters by 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));

Any help is appreciated,thanks

+1  A: 

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
Sorry for UTF-8 to UTF-8 conversion,it is just tryout,I forget like this while writing question.I try different combination such as UTF-8 to Unicode ,Unicode to UTF-8 etc...
slayer35
What I am trying to say is: What happens when you don't do any conversion?
paracycle
without conversion ,characters are still missing.
slayer35
+1  A: 

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
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
+1  A: 

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.

slayer35
+1  A: 

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

Murat
A: 

Plz help me. In our country have a 'Ə' symbol in alphabet. 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); work for turkey but not for azerbaijan. if anyone can help me. plz write to my e-mail. [email protected]

Eli
A: 

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
xoraxbx