Display Gridview with Marathi Data in PDF File using asp.net c#

Mohit Joshi 1 Reputation point
2022-12-26T06:40:18.427+00:00

Hi
I am creating 1 application in which I am populating Data in Marathi language in Gridview. I want to create PDF of this Gridview. I am using iTextSharp to create PDF but it is not showing Marathi Data on that File.
PFB code for PDF : -

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using iTextSharp.tool.xml;
using System.Globalization;
using System.IO;
public void VarReport()
{
//if (DateTime.Now.Day >= 1 && DateTime.Now.Day <= 5 && DateTime.Now.Month % 2 == 0)
{
string filename = "Var_" + dt.GetMonthName(DateTime.Now.Month) + "_" + DateTime.Now.Year+".pdf";
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
VadhuVarData.AllowPaging = false;
getVar();
VadhuVarData.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document doc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(doc);
PdfWriter pw = PdfWriter.GetInstance(doc, Response.OutputStream);
doc.Open();
Paragraph para = new Paragraph();
para.Add("वर यादी");
doc.Add(para);
XMLWorkerHelper.GetInstance().ParseXHtml(pw, doc, sr);
doc.Close();
Response.ContentType = "application/pdf";
Response.ContentEncoding=System.Text.Encoding.UTF8;
Response.AddHeader("content-disposition", "attachment;filename=" + filename);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(doc);
Response.End();
}
}
}
}
Gridview is as in below image
![273907-image.png]2
PDF is showing as in below image
273916-image.png

I kindly request to provide me with exact working code for that.
Thank You

Developer technologies ASP.NET Other
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. QiYou-MSFT 4,326 Reputation points Microsoft External Staff
    2022-12-27T09:04:26.48+00:00

    Hi @Mohit Joshi ,
    The reason for your problem is that itextsharp does not have a Marathi language. But we can add this language manually.
    I'm sorry I don't have Marathi in my system because of the region, but I can show you Chinese way.

    1. First we open the text library of our system.
      It is located at: C:\Windows\Fonts
    2. Create the specified font BaseFont baseFont = BaseFont.CreateFont(
      "C:\WINDOWS\FONTS\Your font file .ttf",
      BaseFont.IDENTITY_H,
      BaseFont.NOT_EMBEDDED);
      iTextSharp.text.Font titleFont = new iTextSharp.text.Font(baseFont, 18);

    3.Introduced System.Text.Encoding.CodePages in Nuget:

    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);  
    

    Best Regards
    Qi You


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.