locked
iTextSharp: Export PDF from GridView can not display in Thai language RRS feed

  • Question

  • User1083136127 posted

       Can I use defined font when I want to export to PDF same adding Paragraph following below?

    --------------------------

      private void ExportToPDF(string strFileName, GridView dg)
        {

           Response.Clear();

            Response.Buffer = true;

            Response.ContentType = "application/pdf";

            Response.AddHeader("content-disposition",

             "attachment;filename=myGridView.pdf");

            Response.Cache.SetCacheability(HttpCacheability.NoCache);

     

            BaseFont EnCodefont = BaseFont.CreateFont(Server.MapPath("/font/ANGSA.TTF"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

            Font Nfont = new Font(EnCodefont, 18, Font.NORMAL);

            StringWriter sw = new StringWriter();

            HtmlTextWriter hw = new HtmlTextWriter(sw);

            myGridView.RenderControl(hw);

            StringReader sr = new StringReader(sw.ToString());

            Document pdfDoc = new Document(PageSize.A4, 30, 20, 40, 40);

            HTMLWorker htmlworker = new HTMLWorker(pdfDoc);

            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

            pdfDoc.Open();

           

            pdfDoc.Add(new Paragraph("สวัสดี", Nfont));   <---- Can Display In Thai Language.

     

            htmlworker.Parse(sr);  <-----  Can Not Display In Thai Language when pulled data from gridview to write down pdf.

     

            pdfDoc.Close();

           Response.Write(pdfDoc);

           Response.End();

     --------------------------------------

     

    I think the default font of iTextsharp is HELVETICA. It can not support Thai language.

     

    Can I add defined font in  "htmlparser.Parse(sr)" ?

     

     

    I would appreciate any ideas.

    Wednesday, January 5, 2011 3:09 AM

Answers

  • User1083136127 posted

    Yo!! I can show Thai language by using code below.

    thank you itext-questions@lists.sourceforge.net  suggest to use FontProvider

    Turkish website coding  http://forum.ceturk.com/archive/index.php/t-22271.html

    ---------------------

    public class MyFontFactoryImpl : FontFactoryImp
        {
            Font defaultFont;
            public MyFontFactoryImpl()
            {

                BaseFont tahoma = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\tahoma.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                defaultFont = new Font(tahoma, 12);
              
            }
            public override Font GetFont(string fontname, string encoding, Boolean embedded, float size, int style, BaseColor color, Boolean cached )
            {
                return defaultFont;
            }
        }

        public class HtmlToPdf
        {
            string _html;
            public HtmlToPdf(string html)
            {
                if (!(FontFactory.FontImp is MyFontFactoryImpl))
                    FontFactory.FontImp = new MyFontFactoryImpl();
                _html = html;
            }

     

            public void Render(Stream stream)
            {
                StringReader sr = new StringReader(_html);
                Document pdfDoc = new Document();
                PdfWriter.GetInstance(pdfDoc, stream);
                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                pdfDoc.Open();
                htmlparser.Parse(sr);
                pdfDoc.Close();
            }

            public void Render(HttpResponse response, string fileName)
            {
                response.Clear();
                response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", fileName));
                response.ContentType = "application/octet-stream";

                Render(response.OutputStream);

                response.End();
            }
        }

     

    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            myGridView.AllowPaging = false;
            myGridView.DataBind();
            myGridView.RenderControl(hw);
           
            HtmlToPdf htmlToPdf = new HtmlToPdf(sw.ToString());

            htmlToPdf.Render(Response, "test.pdf");
        }

    ---------------

    Thank you everybody

     

    http://www.vs2soft.com

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 5, 2011 12:03 PM