Reputation: 3511
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringReader html = new StringReader(sb.ToString());
byte[] byteArray = Encoding.UTF8.GetBytes(sb.ToString());
MemoryStream stream = new MemoryStream(byteArray);
Response.Clear();
using (iTextSharp.text.Document document = new iTextSharp.text.Document())
{
PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(
writer, document, stream, new System.Text.UTF8Encoding()
);
}
Response.End();
So, what could be the reason that pdf doesn't display unicode characted since I have
byte[] byteArray = Encoding.UTF8.GetBytes(sb.ToString());
and
.ParseXHtml(writer, document, stream, new System.Text.UTF8Encoding());
Upvotes: 0
Views: 6244
Reputation: 399
Here is the few steps to display unicode characters in converting Html to Pdf
Check below code
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();
FontFactory.Register("C:\\Windows\\Fonts\\ARIALUNI.TTF", "arial unicode ms");
iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet();
ST.LoadTagStyle("body", "encoding", "Identity-H");
worker.Style = ST;
worker.StartDocument();
Check below link for more understanding....
Hindi, Turkish, and special characters are also display during converting from HTML to PDF using this method. Check below demo image.
Upvotes: 1