通过itextsharp将Html转换为PDF时,阿拉伯语文本显示为空

时间:2017-09-14 13:42:11

标签: c# asp.net itext pdf-generation wkhtmltopdf

我正在尝试将动态生成的HTML转换为pdf文件,它将附加在电子邮件中。它与英文文本一起工作正常但是当我用阿拉伯语替换英语时,它显示空白空间而不是阿拉伯语文本。我尝试过添加字体,并通过挖掘stackoverflow和其他门户网站应用了很多方法,但还没有找到解决方案。

下面是我的代码,我用它生成并附加pdf到电子邮件。

     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();
          }

0 个答案:

没有答案