转换HTML表格数据为PDF时iTextSharp的不显示阿拉伯语数据 [英] ITextsharp not displaying ARABIC data when converting html table data to PDF

查看:204
本文介绍了转换HTML表格数据为PDF时iTextSharp的不显示阿拉伯语数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个report.following是code和sample.I我使用HTML表reports.When我运行成功生成了code的PDF,但阿拉伯语是不是showing.Can你指导我如何我可以嵌入阿拉伯语it.Can修改我的下面code,显示阿拉伯语数据。

  Response.ContentType =应用程序/ PDF
Response.AddHeader(内容处置,附件;文件名= TestPage.pdf);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter的SW =新的StringWriter();
HtmlTextWriter的HW =新的HtmlTextWriter(SW);
tblid1.RenderControl(HW);
StringReader SR =新StringReader(sw.ToString());
文档pdfDoc =新的文档(PageSize.A4,80F,80F,-2F,35F);
HTMLWorker的HTMLParser =新HTMLWorker(pdfDoc);
PdfWriter作家= PdfWriter.GetInstance(pdfDoc,Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(SR);
pdfDoc.Close();
的Response.Write(pdfDoc);
到Response.End();<表ID =tblid1=服务器>&所述; TR>   < TD>سلطانالخارج< / TD>< TD>مسندم< / TD>< / TR>< /表>


解决方案

您需要嵌入字体到您的PDF支持阿拉伯语字型。

 字符串字体路径= Environment.GetEnvironmentVariable(SYSTEMROOT)+\\\\ \\\\字体arabtype.ttf
BASEFONT BASEFONT = BaseFont.CreateFont(字体路径,BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
字体arabicFont =新Font(BASEFONT,10F,Font.NORMAL);

螺纹找到

答: iTextSharp的和阿拉伯字符

修改:这是我如何根据例子我能找到并做到这一点你要完成的任务:

 使用(Web客户端的客户端=新的WebClient()){
   字符串htmlString = client.DownloadString(URL);
}FontFactory.Register(C:/windows/fonts/arabtype.TTF);
风格样式表=新的样式表();
style.LoadTagStyle(身体,面子,阿拉伯字体%的%NAME);
style.LoadTagStyle(身体,编码,BaseFont.IDENTITY_H);
使用(文档的文档=新的文件(PageSize.A4,80F,80F,-2F,35F)){
  PdfWriter作家= PdfWriter.GetInstance(
    文档,Response.OutputStream
  );
  document.Open();
  在HTMLWorker.ParseToList的foreach(IElement元素(
      新StringReader(htmlString),样式))
  {
    document.Add(元件);
  }
}

请注意,您将需要确保您注册一个包含阿拉伯语字符的编码​​正确的TTF文件,并​​且你需要与你正在使用的字体的名称,以取代阿拉伯字体%的%NAME,并更换%变量包含您的原始HTML%与实际的HTML。

I am making a report.following is a code and sample.I am using html table for reports.When I run the code pdf is successfully generated but Arabic is not showing.Can you guide me how can i embed Arabic in it.Can you modify my following code which shows arabic data.

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
tblid1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 80f, 80f, -2f, 35f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

<table id="tblid1" runat="server">

<tr>

   <td>سلطانالخارج</td>

<td>مسندم</td>

</tr>

</table>

解决方案

You would need to embed a font into your pdf that supports arabic glyphs.

string fontpath = Environment.GetEnvironmentVariable( "SystemRoot" ) + "\\fonts\\arabtype.ttf";
BaseFont basefont = BaseFont.CreateFont( fontpath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED );
Font arabicFont = new Font( basefont, 10f, Font.NORMAL );

Answer found in thread: Itextsharp and arabic character !

EDIT: This is how I would do it based on the examples I could find and what you're trying to accomplish:

using(WebClient client = new WebClient()) {
   string htmlString = client.DownloadString(url);
}

FontFactory.Register("c:/windows/fonts/arabtype.TTF"); 
StyleSheet style = new StyleSheet();
style.LoadTagStyle("body", "face", "%NAME OF ARABIC FONT%");
style.LoadTagStyle("body", "encoding", BaseFont.IDENTITY_H);
using (Document document = new Document(PageSize.A4, 80f, 80f, -2f, 35f)) {
  PdfWriter writer = PdfWriter.GetInstance(
    document, Response.OutputStream
  );
  document.Open();
  foreach(IElement element in HTMLWorker.ParseToList(
      new StringReader(htmlString), style))
  {
    document.Add(element);
  }
}

Note that you would need to ensure you are registering the correct TTF file that contains the encoding for arabic characters and that you would need to replace %NAME OF ARABIC FONT% with the name of the font you're using, and replace %VARIABLE CONTAINING YOUR RAW HTML% with the actual HTML.

这篇关于转换HTML表格数据为PDF时iTextSharp的不显示阿拉伯语数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆