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 language).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;
}
}