Click here to Skip to main content
15,880,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I Just try to export html code into pdf with unicode. My code is working fine if my html does not contain any unicode. could someone suggest me how can i do it for unicode also.

What I have tried:

protected void btnClick_Click(object sender, EventArgs e)
        {
            DownloadAsPDF();
        }
        [Obsolete]
        public void DownloadAsPDF()
        {
            try
            {
                string strHtml = string.Empty;
                string pdfFileName = Request.PhysicalApplicationPath + "\\files\\" + "GenerateHTMLTOPDF.pdf";
                StringWriter sw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw);
                dvHtml.RenderControl(hw);
                StringReader sr = new StringReader(sw.ToString());
                strHtml = sr.ReadToEnd();
                sr.Close();
                CreatePDFFromHTMLFile("<pre><div style='font - family: arial unicode ms; '>" + strHtml + "</div>
", pdfFileName);
Response.ContentType = "application/x-download";
Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", "GenerateHTMLTOPDF.pdf"));
Response.ContentEncoding = Encoding.UTF8;
Response.WriteFile(pdfFileName);
Response.HeaderEncoding = Encoding.UTF8;
Response.Flush();
Response.End();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}


[Obsolete]
       private void CreatePDFFromHTMLFile(string html, string FileName)
       {
           TextReader reader = new StringReader(html);
           // step 1: creation of a document-object
           Document document = new Document(PageSize.A4, 30, 30, 30, 30);
           // step 2:
           // we create a writer that listens to the document
           // and directs a XML-stream to a file
           PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(FileName, FileMode.Create));
           // step 3: we create a worker parse the document
           HTMLWorker worker = new HTMLWorker(document);
           // step 4: we open document and start the worker on the document
           document.Open();
           // step 4.1: register a unicode font and assign it an allias
           //FontFactory.Register("C:\\Windows\\Fonts\\ARIALUNI.TTF", "arial unicode ms");
           string fontPath = Server.MapPath("~/fonts/");
           BaseFont bf = BaseFont.CreateFont(fontPath + "ARIALUNI.ttf", BaseFont.IDENTITY_H, true);
           // step 4.2: create a style sheet and set the encoding to Identity-H
           iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet();
           ST.LoadTagStyle("body", "encoding", "Identity-H");
           // step 4.3: assign the style sheet to the html parser
           worker.SetStyleSheet(ST);
           worker.StartDocument();
           // step 5: parse the html into the document
           worker.Parse(reader);
           // step 6: close the document and the worker
           worker.EndDocument();
           worker.Close();
           document.Close();
       }
Posted
Updated 12-Apr-21 2:12am
v3
Comments
Arfat M 12-Apr-21 8:15am    
Kindly check this you may get solution for your question :
http://codescratcher.com/asp-net/display-unicode-characters-in-converting-html-to-pdf/
TCS54321 12-Apr-21 8:25am    
i already used the same code of this link. it's not working for unicode.
Arfat M 12-Apr-21 8:35am    
check step 4.1 in above link : FontFactory.Register("C:\\Windows\\Fonts\\ARIALUNI.TTF", "arial unicode ms");
TCS54321 12-Apr-21 8:59am    
i install this font also. but it's not working.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900