0

I am trying to convert dynamically generated HTML to a pdf file and it will be attached in email. It works fine with the text in English but when i replace English with Arabic, it shows empty space instead of Arabic text. I have tried adding font and also applied many ways by digging out stackoverflow and other portals but couldn't find a solution yet.

Below here is my code which i am using to generate and attach pdf to email.

     StringBuilder sb = new StringBuilder();
                        sb.Append("<table style='max-width:600px; margin: 0 auto;' border='0' cellpadding='0' cellspacing='0' dir='rtl' style='text-align: right;'>");
                        sb.Append("<tbody>");
                        sb.Append("<tr><td colspan='4' width='100%' align='center' valign='top'></td> </tr>");
        sb.Append("<tr class='abc'><td colspan='4' width='100%' align='right' valign='top'><h3 style='font-family: 'Noto Kufi Arabic', sans-serif; font-size: 24px; margin: 10px 0 10px 0; color: #216a5e;'>name</h3></td> </tr>");
                        sb.Append("<tr class='abc'><td colspan='4' width='100%' align='right' valign='top' style='border: 1px solid #216a5e; padding: 10px; font-family: 'Noto Kufi Arabic', sans-serif;><b style='color: #216a5e;'>شركاء النجاح</b><span style='margin:0 20px;'>aaa</span></td> </tr>");

                        sb.Append("<tr><td colspan='4' width='100%' style='padding: 5px;'></td> </tr>");
                        sb.Append("<tr><td width='50%' align='right' valign='top' style='border: 1px solid #216a5e; padding: 10px; font-family: 'Noto Kufi Arabic';'><b style='color: #216a5e;'>phone</b><br /><span class='test'>0096650540</span> </td>");
                        sb.Append("<td width='50%' align='right' valign='top' style='border: 1px solid #216a5e; padding: 10px; font-family: 'Noto Kufi Arabic', sans-serif;'><b style='color: #216a5e;'>branch nae</b><br /><span>شركاء النجاح</span> </td></tr>");
         sb.Append("</tbody>");
        sb.Append("</table>");


    StringReader sr = new StringReader(sb.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);

    using (MemoryStream memoryStream = new MemoryStream())
    {
     PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
     HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
     htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());


 ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);

    cssResolver.AddCss("tr.abc{color: red;}", true);
    cssResolver.AddCss(".test{color: yellow;}", true);
    pdfDoc.Open();
                        writer.DirectContent.SetFontAndSize(GetFont("NotoKufiArabic"), 24f);
 IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(pdfDoc, writer)));

 XMLWorker worker = new XMLWorker(pipeline, true);
 XMLParser xmlParser = new XMLParser(worker);         
 StyleSheet style = new StyleSheet();
 style.LoadTagStyle("body", "face", Server.MapPath("~/Styles/test.css"));
 style.LoadTagStyle("body", "face", "Noto Kufi Arabic");
 style.LoadTagStyle("body", "encoding", BaseFont.IDENTITY_H);

 foreach (IElement element in HTMLWorker.ParseToList(
 new StringReader(sr.ToString()), style))
 {
    pdfDoc.Add(element);
 }

  xmlParser.Parse(sr);
  pdfDoc.Close();
   byte[] bytes = memoryStream.ToArray();
   memoryStream.Close();

   MailAddress from = new 
   MailAddress(ConfigurationManager.AppSettings["SendEmailFrom"]);
   MailAddress to = new MailAddress("test@yahoo.com");
   MailMessage message = new MailMessage(from, to);

   message.Attachments.Add(new Attachment(new MemoryStream(bytes), 
  "EnsanPDF.pdf"));
   message.Subject = "iTextSharp PDF";
   message.IsBodyHtml = true;
   message.Body = "";
   Thread email = new Thread(delegate()
   {
   SmtpClient client = new 
   SmtpClient(ConfigurationManager.AppSettings["MailAddress"]);
   client.Port = int.Parse(ConfigurationManager.AppSettings["Port"].ToString());
   client.Credentials = new 
   NetworkCredential(ConfigurationManager.AppSettings["AuthorizeEmail"], 
   ConfigurationManager.AppSettings["AuthorizeEmailPassword"]);
           client.EnableSsl = true;
           client.Send(message);
             });
           email.IsBackground = true;
            email.Start();
          }
Muhammad Bilal
  • 125
  • 1
  • 9
  • It's as if you've taken some code found in the wild on the internet, put everything in a bag, shaken very hard, and then tried to run the result. It should be obvious that this doesn't work. Please read [this chapter](https://developers.itextpdf.com/content/itext-7-converting-html-pdf-pdfhtml/chapter-6-using-fonts-pdfhtml) and look for the word "Arabic". In this tutorial, we start by "doing it wrong" (as if we didn't know anything about iText), then we fix different problems step by step. Eventually, we are able to convert Arabic text to PDF. – Bruno Lowagie Sep 14 '17 at 13:57
  • Since so many iText users fail to read the documentation, we've also added the following FAQ items: [How to convert HTML containing Arabic/Hebrew characters to PDF?](https://developers.itextpdf.com/content/itext-7-converting-html-pdf-pdfhtml/chapter-7-frequently-asked-questions-about-pdfhtml/how-convert-html-containing-arabichebrew-characters-pdf) and [Which languages are supported in pdfHTML?](https://developers.itextpdf.com/content/itext-7-converting-html-pdf-pdfhtml/chapter-7-frequently-asked-questions-about-pdfhtml/which-languages-are-supported-pdfhtml) – Bruno Lowagie Sep 14 '17 at 13:58
  • Note that you're using an old version of iText, and that the links in the comments above require the use of iText 7 for .NET and the pdfHTML add-on. However, If, for some reason, you want to use an old version of iText, please take a look at the link that I used to close your question. That link point to Java code, but I'm told that it's very easy for a .NET developer to port that code to C#. – Bruno Lowagie Sep 14 '17 at 14:00

0 Answers0