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

Posted by Cpatil1000 under ASP.NET on 9/20/2016 | Points: 10 | Views : 684 | Status : [Member] | Replies : 2
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:

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





Responses

Posted by: Rajnilari2015 on: 9/20/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
You could use ArPDF, which supports Hindi script. http://www.winhpp.net/arpdf .

--
Thanks & Regards,
RNA Team

Cpatil1000, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Susan2016 on: 9/23/2016 [Member] Starter | Points: 25

Up
0
Down
Embedding private fonts(Hindi) when creating PDF document is also a solution.

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

Cpatil1000, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response