0

I convert my view page in .net to pdf and use itext I have Arabic and English words Arabic is not appears at all I don't Know what is the problem

  [HttpPost]
        [ValidateInput(false)]
        public FileResult Export(string GridHtml, string name , string id)
        {
            HtmlNode.ElementsFlags["img"] = HtmlElementFlag.Closed;
            HtmlNode.ElementsFlags["input"] = HtmlElementFlag.Closed;
            HtmlNode.ElementsFlags["hr"] = HtmlElementFlag.Closed;

            HtmlDocument doc = new HtmlDocument();
            doc.OptionFixNestedTags = true;
            doc.LoadHtml(GridHtml);
            GridHtml = doc.DocumentNode.OuterHtml;

            
            using (MemoryStream stream = new System.IO.MemoryStream())
            {
                StringReader sr = new StringReader(GridHtml);
                
                Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
                PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
       
                 pdfDoc.Open();
                XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
                
                pdfDoc.Close();
                return File(stream.ToArray(), "application/pdf", string.Format("\"{0}\".pdf", name));

            }
        }

this is view page code

 @using (Html.BeginForm("Export", "InvoiceUser", FormMethod.Post))
                            {
                                <input type="hidden"name="GridHtml" />
                                <input type="hidden"  name="name" value="@business.CompanyName||@invoice.InvoiceID" />
                               
                                <input type="submit" id="btnSubmit"  value="Download" class="btn btn-inverse waves-effect waves-light" />
                                <button type="button" onclick="close('myModal')" class="btn btn-inverse waves-effect waves-light" data-dismiss="modal">Close</button>

                            }
  • How about to add `new Phrase("Arabic Text", Font)` to convert the Arabic Text? You may get some help from [here](https://stackoverflow.com/a/34529735/9862794). – DasiyTian_1203 Feb 11 '21 at 06:24
  • @DasiyTianMSFT I tried this but this solution is not work for me – salma alalawi Feb 11 '21 at 09:48
  • What the version of your iText? Is it [iText7](https://kb.itextpdf.com/home/it7kb/installation-guidelines/installing-itext-7-for-net) There's an example involving Arabic in the section entitled [Internationalization](https://kb.itextpdf.com/home/it7kb/ebooks/itext-7-converting-html-to-pdf-with-pdfhtml/chapter-6-using-fonts-in-pdfhtml#Chapter6:UsingfontsinpdfHTML-Internationalization) which may give you some help. – DasiyTian_1203 Feb 12 '21 at 07:11
  • @DasiyTianMSFT it is itext5 not 7 – salma alalawi Feb 16 '21 at 07:51

1 Answers1

0

Try adding charset also while parsing

XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr, Encoding.UTF8);

OR

You can also add Font Provider as an argument like in this example Display Unicode characters in converting Html to Pdf

shihabudheenk
  • 593
  • 5
  • 18