Export arabic or parsi languages to PDF using itextSharp in ASP.Net

Last Reply one year ago By dharmendr

Posted one year ago

here is the code that i used 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DivToPdf.aspx.cs" Inherits="divtopdf.DivToPdf" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div class="lead" id="MyDiv" runat="server">
            <p>
                Each package is licensed to you by its owner. NuGet is not responsible for, nor does it grant any licenses to, third-party packages. Some packages may include dependencies which are governed by additional licenses. Follow the package source (feed) URL to determine any dependencies.
Package Manager Console Host Version 4.0.0.2323
Type 'get-help NuGet' to see all available NuGet commands.
            </p>
        </div>
        <div class="lead" style="direction:rtl;">
            <p> لاوروف: آخرین ذره اعتمادمان به غرب را هم داریم از دست می‌دهیم</p>
        </div>

        <asp:Button ID="btnConvert" Text="Convert" runat="server" OnClick="btnConvert_Click" />
    </form>
</body>
</html>

 

and this is the button event based onclick event

 

protected void btnConvert_Click(object sender, EventArgs e)
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=div.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);


            StringWriter stringWriter = new StringWriter();
            HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
            MyDiv.RenderControl(htmlTextWriter);


            StringReader stringReader = new StringReader(stringWriter.ToString());
            Document Doc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(Doc);
            PdfWriter.GetInstance(Doc, Response.OutputStream);

            Doc.Open();
            htmlparser.Parse(stringReader);
            Doc.Close();
            Response.Write(Doc);
            Response.End();
        }

 

You are viewing reply posted by: dharmendr one year ago.
Posted one year ago

Hi Mohammadmk,

Important for Arabic, Persian or Urdu Text you need to assign RUN_DIRECTION_RTL.

For more details refer below article.

iTextSharp: Generate PDF in Arabic, Persian (Farsi) and Urdu Language Characters in ASP.Net