levi
levi

Reputation: 3511

Html Converted into PDF Not Showing Unicode Characters

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

Answers (1)

Code Scratcher
Code Scratcher

Reputation: 399

Here is the few steps to display unicode characters in converting Html to Pdf

  1. Create a HTMLWorker
  2. Register a unicode font and assign it
  3. Create a style sheet and set the encoding to Identity-H
  4. Assign the style sheet to the html parser
  5. 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.

enter image description here

Upvotes: 1

Related Questions