如何从带有阿拉伯字符的html字符串创建PDF文档并附加到邮件 [英] How to Create PDF Document from html string with arabic characters and attach to mail
本文介绍了如何从带有阿拉伯字符的html字符串创建PDF文档并附加到邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
我已经从html字符串创建了PDF文档。但阿拉伯语没有在pdf中显示任何人可以帮我解决这个问题。
Hello,
I havecreated PDF document from html string. but the arabic is not showing in the pdf can any one help me to solve this.
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("OrderId"),
new DataColumn("Product"),
new DataColumn("Quanity")});
dt.Rows.Add(101, "فيشنو", 5);
dt.Rows.Add(102, "Jeans", 2);
dt.Rows.Add(103, "Trousers", 12);
dt.Rows.Add(104, "Shirts", 9);
SendPDFEmail(dt);
private void SendPDFEmail(DataTable dt)
{
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
string companyName = "ASPSnippets";
int orderNo = 2303;
StringBuilder sb = new StringBuilder();
sb.Append("<table width='100%' cellspacing='0' cellpadding='2'>");
sb.Append("<tr><td align='center' style='background-color: #18B5F0' colspan = '2'><b>Order Sheet</b></td></tr>");
sb.Append("<tr><td colspan = '2'></td></tr>");
sb.Append("<tr><td><b>Order No:</b>");
sb.Append(orderNo);
sb.Append("</td><td><b>Date: </b>");
sb.Append(DateTime.Now);
sb.Append(" </td></tr>");
sb.Append("<tr><td colspan = '2'><b>Company Name :</b> ");
sb.Append(companyName);
sb.Append("</td></tr>");
sb.Append("</table>");
sb.Append("<br />");
sb.Append("<table border = '1'>");
sb.Append("<tr>");
foreach (DataColumn column in dt.Columns)
{
sb.Append("<th style = 'background-color: #D20B0C;color:#ffffff'>");
sb.Append(column.ColumnName);
sb.Append("</th>");
}
sb.Append("</tr>");
foreach (DataRow row in dt.Rows)
{
sb.Append("<tr>");
foreach (DataColumn column in dt.Columns)
{
sb.Append("<td>");
sb.Append(row[column]);
sb.Append("</td>");
}
sb.Append("</tr>");
}
sb.Append("</table>");
StringReader sr = new StringReader(sb.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
using (MemoryStream memoryStream = new MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
var arialFontPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF");
FontFactory.Register(arialFontPath);
BaseFont bf = BaseFont.CreateFont(arialFontPath, BaseFont.IDENTITY_H, true);
iTextSharp.text.Font FontAr = new iTextSharp.text.Font(bf);
iTextSharp.text.FontFactory.RegisterDirectory(arialFontPath);
StyleSheet styles = new StyleSheet();
styles.LoadTagStyle("body", "face", "Arial Unicode MS");
var htmlarraylist = HTMLWorker.ParseToList(sr, styles).ToArray();
for (int k = 0; k < htmlarraylist.Count(); k++)
{
pdfDoc.Add((IElement)htmlarraylist[k]);
}
htmlparser.Parse(sr);
pdfDoc.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add("zzzz@gmail.com");
mail.From = new MailAddress("xyxyx@gmail.com", "PDF", System.Text.Encoding.UTF8);
mail.Subject = "iTextSharp PDF";
mail.Body = "iTextSharp PDF Attachment";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.Attachments.Add(new Attachment(new MemoryStream(bytes), "iTextSharpPDF.pdf"));
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("xxxx@gmail.com", "xxxx123");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
try
{
Thread threadmail = new Thread(delegate()
{
client.Send(mail);
});
threadmail.IsBackground = true;
threadmail.Start();
}
catch (Exception ex)
{
Exception ex2 = ex;
string errorMessage = string.Empty;
while (ex2 != null)
{
errorMessage += ex2.ToString();
ex2 = ex2.InnerException;
}
}
}
}
}
}
推荐答案
Try setting the encoding as well as the font face:
Try setting the encoding as well as the font face:
StyleSheet styles = new StyleSheet();
styles.LoadTagStyle("body", "face", "Arial Unicode MS");
styles.LoadTagStyle("body", "encoding", BaseFont.IDENTITY_H);
Also, the ParseToList
method returns a List<IElement>
; there’s no need to convert it to an array:
Also, the ParseToList
method returns a List<IElement>
; there's no need to convert it to an array:
var elements = HTMLWorker.ParseToList(sr, styles);
foreach (IElement element in elements)
{
pdfDoc.Add(element);
}
I suspect you don’t need the htmlparser.Parse(sr);
line, since you’ve already parsed the content with the ParseToList
call.
I suspect you don't need the htmlparser.Parse(sr);
line, since you've already parsed the content with the ParseToList
call.
这篇关于如何从带有阿拉伯字符的html字符串创建PDF文档并附加到邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文