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();
        }

 

Posted one year ago

I am working on it i will get back to you soon.


Posted one year ago

Hi Mohammadmk,

Check this example. Now please take its reference and correct your code.

HTML

<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 class="lead" style="direction: rtl;">
        <p>
            لاوروف: آخرین ذره اعتمادمان به غرب را هم داریم از دست می‌دهیم</p>
    </div>
</div>
<asp:Button ID="btnConvert" Text="Convert" runat="server" OnClick="btnConvert_Click" />

Namespaces

C#

using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System.Text;

VB.Net

Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.html.simpleparser
Imports iTextSharp.text.pdf
Imports System.Text

Code

C#

protected void btnConvert_Click(object sender, EventArgs e)
{
    string strHtml = string.Empty;
    string pdfFileName = Server.MapPath("~/files/div.pdf");
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    MyDiv.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    strHtml = sr.ReadToEnd();
    sr.Close();
    CreatePDFFromHTMLFile("<div style='font-family:arial unicode ms;'>" + strHtml + "</div>", pdfFileName);
    Response.ContentType = "application/pdf";
    Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", "div.pdf"));
    Response.ContentEncoding = Encoding.UTF8;
    Response.WriteFile(pdfFileName);
    Response.HeaderEncoding = Encoding.UTF8;
    Response.Flush();
    Response.End();
}

private void CreatePDFFromHTMLFile(string html, string FileName)
{
    TextReader reader = new StringReader(html);
    Document document = new Document(PageSize.A4, 30, 30, 30, 30);
    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(FileName, FileMode.Create));
    HTMLWorker worker = new HTMLWorker(document);
    document.Open();
    FontFactory.Register("C:\\Windows\\Fonts\\ARIALUNI.TTF", "arial unicode ms");
    iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet();
    ST.LoadTagStyle("body", "encoding", "Identity-H");
    worker.SetStyleSheet(ST);
    worker.StartDocument();
    worker.Parse(reader);
    worker.EndDocument();
    worker.Close();
    document.Close();
}

VB.Net

Protected Sub btnConvert_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim strHtml As String = String.Empty
    Dim pdfFileName As String = Server.MapPath("~/files/div.pdf")
    Dim sw As StringWriter = New StringWriter()
    Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)
    MyDiv.RenderControl(hw)
    Dim sr As StringReader = New StringReader(sw.ToString())
    strHtml = sr.ReadToEnd()
    sr.Close()
    CreatePDFFromHTMLFile("<div style='font-family:arial unicode ms;'>" & strHtml & "</div>", pdfFileName)
    Response.ContentType = "application/pdf"
    Response.AddHeader("Content-Disposition", String.Format("attachment; filename=""{0}""", "div.pdf"))
    Response.ContentEncoding = Encoding.UTF8
    Response.WriteFile(pdfFileName)
    Response.HeaderEncoding = Encoding.UTF8
    Response.Flush()
    Response.[End]()
End Sub

Private Sub CreatePDFFromHTMLFile(ByVal html As String, ByVal FileName As String)
    Dim reader As TextReader = New StringReader(html)
    Dim document As Document = New Document(PageSize.A4, 30, 30, 30, 30)
    Dim writer As PdfWriter = PdfWriter.GetInstance(document, New FileStream(FileName, FileMode.Create))
    Dim worker As HTMLWorker = New HTMLWorker(document)
    document.Open()
    FontFactory.Register("C:\Windows\Fonts\ARIALUNI.TTF", "arial unicode ms")
    Dim ST As iTextSharp.text.html.simpleparser.StyleSheet = New iTextSharp.text.html.simpleparser.StyleSheet()
    ST.LoadTagStyle("body", "encoding", "Identity-H")
    worker.SetStyleSheet(ST)
    worker.StartDocument()
    worker.Parse(reader)
    worker.EndDocument()
    worker.Close()
    document.Close()
End Sub

Screenshot

Refering below link i have created the example.

https://stackoverflow.com/questions/10329863/display-unicode-characters-in-converting-html-to-pdf

 


Posted one year ago

thanks for the help 

but here is a small issue while we are converting div into pdf format file

the sentence which is in persian or arabic both are not the same 

 

in the pdf  each word are sepetated into single single alphabits  for example  

this is the actual word  "اعتمادمان"  and after converting to pdf the word is like this "اع ت م ادم ان"  in arabic or persian languages most of the alphabits are clinged to each other  

 


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