You must Sign In to post a response.
  • Category: ASP.NET

    How to Create PDF file with Hindi Font by using itextsharp in C#?

    we are creating pdf files in our C# application by passing HINDI text(one of the indian l
    anguage).So, I already installed ARIALUNI.TTF(one of the Hindi font) font for my Hindi
    language font.But when i run the below mentioned pgm, the created pdf file does not contain
    any Hindi font display.
    It shows empty lines instead of the Hindi text...

    C# Code:

    protected void lbFarmerPdf_Click(object sender, EventArgs e)
    {
    int intVal, intVal1, intVal2;
    string cellText, cellText1;

    try
    {
    this.SetFarmerList();

    dgFarmerDetail.AllowPaging = false;
    dgFarmerDetail.DataBind();

    //BaseFont bf = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") + @"\fonts\ARIALUNI.TTF", BaseFont.CP1252, BaseFont.EMBEDDED);

    string fontpath = Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\ARIALUNI.TTF";
    BaseFont bf = BaseFont.CreateFont(fontpath, BaseFont.IDENTITY_H, true);

    this.dFarmerDetail.Visible = true;

    this.dReportFarmerHeader.Visible = true;

    Response.ContentType = "application/pdf";
    Response.ContentEncoding = System.Text.Encoding.UTF8;
    Response.AddHeader("content-disposition", "attachment;filename=FarmerStatus_" + DateTime.Now.Date.ToString("dd-MM-yyyy") + ".pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);

    PdfPTable table = new PdfPTable(dgFarmerDetail.Columns.Count);
    int[] widths = new int[dgFarmerDetail.Columns.Count];

    for (intVal = 0; intVal < dgFarmerDetail.Columns.Count; intVal++)
    {
    widths[intVal] = (int)dgFarmerDetail.Columns[intVal].ItemStyle.Width.Value;
    cellText = Server.HtmlDecode(dgFarmerDetail.Columns[intVal].HeaderText);

    //Set Font and Font Color
    Font font = new Font(bf, 10, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLUE);
    font.Color = new Color(dgFarmerDetail.HeaderStyle.ForeColor);
    PdfPCell cell = new PdfPCell(new Phrase(24, cellText, font));

    //Set Header Row BackGround Color
    cell.BackgroundColor = new Color(dgFarmerDetail.HeaderStyle.BackColor);
    table.AddCell(cell);
    }

    table.SetWidths(widths);

    for (intVal1 = 0; intVal1 < dgFarmerDetail.Items.Count; intVal1++)
    {
    if (dgFarmerDetail.Items[intVal1].ItemType == ListItemType.Item || dgFarmerDetail.Items[intVal1].ItemType == ListItemType.AlternatingItem)
    {
    for (intVal2 = 0; intVal2 < dgFarmerDetail.Columns.Count; intVal2++)
    {
    cellText1 = Server.HtmlDecode(dgFarmerDetail.Items[intVal1].Cells[intVal2].Text);
    //Set Font and Font Color
    Font font = new Font(bf, 10, Font.NORMAL);
    font.Color = new Color(dgFarmerDetail.ItemStyle.ForeColor);
    PdfPCell cell = new PdfPCell(new Phrase(12, cellText1, font));
    table.AddCell(cell);
    }
    }
    }

    this.dgFarmerDetail.RenderControl(hw);

    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(new Rectangle(2500f, 800f));
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
    }
    catch (Exception ex)
    {
    throw ex;
    }
    finally
    {
    this.dgFarmerDetail.AllowPaging = true;
    this.farmerHead.Visible = false;
    }
    }
  • #767884
    Unless you embed fonts in pdf you are unable to see it on PDF fonts, try to embed fonts in PDF using ItextSharp
    see below snippet to embed fonts

    bfR = iTextSharp.text.pdf.BaseFont.CreateFont(FontFile, iTextSharp.text.pdf.BaseFont.IDENTITY_H, iTextSharp.text.pdf.BaseFont.EMBEDDED)
    bfR.Subset = False

    switch to below link for more details
    http://stackoverflow.com/questions/4231656/how-do-i-embed-fonts-in-an-existing-pdf

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #767916
    Hi Chandrasekar,

    The important thing in doing this Hindi language PDF is the TIFF file.
    Most commonly used TIFF file is ARIALUNI.TTF



    try
    {
    string fontpath = Environment.GetEnvironmentVariable("SystemRootFolders") + "\\fonts\\ARIALUNI.TTF";
    BaseFont bf = BaseFont.CreateFont(fontpath, BaseFont.IDENTITY_H, true);

    Response.ContentType = "application/pdf";
    Response.ContentEncoding = System.Text.Encoding.UTF8;

    ------Remaining Logics here----------------------

    }
    Catch
    {
    }

    Thanks,
    Mani

  • #767929
    Hi Chandrashekhar,

    I think the following link would be helpful:

    http://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Text/Embed-private-font-to-pdf-document-via-Spire.PDF.html


  • Sign In to post your comments