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);
Document document = new Document(PageSize.A4, 30, 30, 30, 30);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(FileName, FileMode.Create));
HTMLWorker worker = new HTMLWorker(document);
document.Open();
string fontPath = Server.MapPath("~/fonts/");
BaseFont bf = BaseFont.CreateFont(fontPath + "ARIALUNI.ttf", BaseFont.IDENTITY_H, true);
iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet();
ST.LoadTagStyle("body", "encoding", "Identity-H");
worker.SetStyleSheet(ST);
worker.StartDocument();
worker.Parse(reader);
worker.EndDocument();
worker.Close();
document.Close();
}