0

Hello I've been trying to convert a HTML string to PDF in iText, it works and I get the file but any arabic text in the HTML is now showing, ofcourse I searched for a solution and found out that I need to register a font for arabic and I tried more than one font but no luck at all, here is my code and any help would be great

private void CreatePDFItextSharp()
        {
            string dataFile = Server.MapPath("~/Html.txt");
            string html = File.ReadAllText(dataFile);

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

            //Font normal = FontFactory.GetFont("c:/windows/fonts/arial.ttf", BaseFont.IDENTITY_H, 18, Font.BOLD);
            string arialFontFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "arial.ttf");
            FontFactory.Register(arialFontFile);

            XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
            fontProvider.Register(arialFontFile);

            byte[] bytes;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
                pdfDoc.Open();

                var xmlWorker = XMLWorkerHelper.GetInstance();
                MemoryStream htmlContent = new MemoryStream(Encoding.UTF8.GetBytes(html));
                xmlWorker.ParseXHtml(writer, pdfDoc, htmlContent, null, Encoding.UTF8, fontProvider);

                pdfDoc.Close();

                bytes = memoryStream.ToArray();
                memoryStream.Close();
            }

            Response.Clear();
            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-Disposition", "attachment; filename=Invoice.pdf");
            Response.Buffer = true;
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.BinaryWrite(bytes);
            Response.End();
            Response.Close();
        }
Hasan
  • 57
  • 1
  • 9
  • 1
    Try and check this link https://stackoverflow.com/questions/31357181/how-to-display-arabic-in-pdf-created-using-itext – S_G Nov 02 '22 at 14:22

2 Answers2

1

I had a similar problem and I think you are on the right track

Try using Arial Unicode MS (https://learn.microsoft.com/en-us/typography/font-list/arial-unicode-ms). I had to include this in my application installer.

barrett777
  • 282
  • 3
  • 14
  • I changed the font to use the "Arialuni.ttf" but that didnt work, I even changed the XMLWorkerFontProvider to use all fonts and it didn't work either like this: `XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(Environment.GetFolderPath(Environment.SpecialFolder.Fonts)); fontProvider.UseUnicode = true;` and I even saw that the XMLWorkerFontProvider have all the fonts in the system but still the arabic text won't show – Hasan Nov 06 '22 at 05:54
0

I finlly got it to work, turns out that I have to register the font and use it in the HTML text.

this line to register the fonts in this case I'll register all fonts XMLWorkerFontProvider(Environment.GetFolderPath(Environment.SpecialFolder.Fonts));

and in the html I should have something like this:

<body style="font-family:'Arial Unicode MS'"> to use the font

Hasan
  • 57
  • 1
  • 9