0

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>
user3878407
  • 1
  • 2
  • 5
  • possible duplicate of [Display Unicode characters in converting Html to Pdf](http://stackoverflow.com/questions/10329863/display-unicode-characters-in-converting-html-to-pdf) – Chris Haas Jul 25 '14 at 22:55
  • I am not gentrating html using string builder.My html is in .aspx page not in the code behind file – user3878407 Jul 25 '14 at 23:10
  • Doesn't matter. iTextSharp has no concept of ASP.Net, controls, code-behind, etc. You need to feed it a string that represents valid HTML. You are also using the old and no longer maintained HTMLWorker, I would recommend that you switch to XMLWorker. – Chris Haas Jul 26 '14 at 19:52
  • Is XMLWORKER can convert html to pdf? – user3878407 Jul 26 '14 at 19:55
  • Yes. Although the name is deceiving it is where all HTML and CSS parsing efforts are being placed. – Chris Haas Jul 26 '14 at 20:03
  • I am add refrence of iTextSharp.xml in visual studio for xmlWorker.A messagebox appear itextsharp.xml could not be added which refrence I will use for using in asp.net c# – user3878407 Jul 26 '14 at 20:13
  • using iTextSharp.text.xml.simpleparser; BUT xml worker not found – user3878407 Jul 26 '14 at 20:15
  • check this question that may help you http://stackoverflow.com/questions/16080741/convert-arabicunicode-content-html-or-xml-to-pdf-using-itextsharp – Mohamed Salah Jul 27 '15 at 14:40

2 Answers2

1

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.

Community
  • 1
  • 1
c0r3yz
  • 835
  • 8
  • 19
0
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(HtmlTags.DIV, HtmlTags.FONTSIZE, "16");
            styles.LoadTagStyle(HtmlTags.DIV, HtmlTags.COLOR, "navy");
            styles.LoadTagStyle(HtmlTags.DIV, HtmlTags.FONTWEIGHT, "bold");
            styles.LoadTagStyle(HtmlTags.P, HtmlTags.INDENT, "30px");
            styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.FACE, "Arial Unicode MS");
            styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H);

 List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(htmlText), styles);
                for (int k = 0; k < htmlarraylist.Count; k++)
                {
                    pdfDocument.Add((IElement)htmlarraylist[k]);
                }

this piece of code worked for me, arabic language works perfect in this, just pass your html text to my htmltext Variable..

Hopefully it will work fine.

Hudhaifa Yoosuf
  • 869
  • 2
  • 12
  • 28