Geeks With Blogs
Jayd Page

 

Introduction

iTextSharp is a useful library for creating PDF documents in .Net. However because of the right to left nature of Hebrew and Arabic texts, adding them to the document normally through iTextSharp renders them unreadable.
Lets take a look at how we can solve this problem.

Download iTextSharp here

Please read the comments to guide you through the code.

Code

using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Text.RegularExpressions;
using System.IO;
using System.Diagnostics;

public void WriteDocument()
{
  
//Declare a itextSharp document
    Document document = new Document(PageSize.A4);

    //Create our file stream and bind the writer to the document and the stream
    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(@"C:\Test.Pdf", FileMode.Create));

    //Open the document for writing
    document.Open();

   //Add a new page
    document.NewPage();

    //Reference a Unicode font to be sure that the symbols are present.
    BaseFont bfArialUniCode = BaseFont.CreateFont(@"C:\ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    //Create a font from the base font
    Font font = new Font(bfArialUniCode, 12);

    //Use a table so that we can set the text direction
    PdfPTable table = new PdfPTable(1);
    //Ensure that wrapping is on, otherwise Right to Left text will not display
    table.DefaultCell.NoWrap = false;

    //Create a regex expression to detect hebrew or arabic code points
    const string regex_match_arabic_hebrew = @"[\u0600-\u06FF,\u0590-\u05FF]+";
    if (Regex.IsMatch("מה קורה", regex_match_arabic_hebrew, RegexOptions.IgnoreCase))
    {
        table.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
    }

    //Create a cell and add text to it
    PdfPCell text = new PdfPCell(new Phrase("מה קורה", font));
    //Ensure that wrapping is on, otherwise Right to Left text will not display
    text.NoWrap = false;

    //Add the cell to the table
    table.AddCell(text);

    //Add the table to the document
    document.Add(table);

    //Close the document
    document.Close();

    //Launch the document if you have a file association set for PDF's
    Process AcrobatReader = new Process();
    AcrobatReader.StartInfo.FileName = @"C:\Test.Pdf";
    AcrobatReader.Start();

}

Conclusion

We used a regex expression to detect if the text was Hebrew or Arabic and then set the .RunDirection of the PdfpTable or PdfPCell accordingly.
Ensure that .NoWrap is set to false on the default cell of the table and any cells that you may create manually, if wrapping is turned off the right to left text will not be displayed.

Posted on Wednesday, November 2, 2011 4:10 AM | Back to top


Comments on this post: Using iTextSharp to correctly display Hebrew / Arabic text (Right to Left) in a PDF Document

# re: Using iTextSharp to correctly display Hebrew / Arabic text (Right to Left) in a PDF Document
Requesting Gravatar...
if(Regex.IsMatch ...) then set RTL? right?
and pdfpcell has that RunDirection too.
Left by Anon on Nov 02, 2011 9:09 AM

# re: Using iTextSharp to correctly display Hebrew / Arabic text (Right to Left) in a PDF Document
Requesting Gravatar...
Yes, thanks Anon. I have fixed the RunDirection on the table, No need to set it on the cell because it is a child of the table, however if one needed to do so then it is possible.
Left by Jayd on Nov 02, 2011 6:02 PM

# re: Using iTextSharp to correctly display Hebrew / Arabic text (Right to Left) in a PDF Document
Requesting Gravatar...
Thanks a bunch! Your NoWrap trick fixed my problem! Arabic text was not displaying and I was wondering why :-)
Left by Abdo on Feb 06, 2012 7:42 AM

# re: Using iTextSharp to correctly display Hebrew / Arabic text (Right to Left) in a PDF Document
Requesting Gravatar...
Thanks , you helped me a lot .
Left by Fayed on Apr 02, 2012 7:45 PM

# re: Using iTextSharp to correctly display Hebrew / Arabic text (Right to Left) in a PDF Document
Requesting Gravatar...
Hey thanks a lot for this post, i just want to know one thing in my application i am using ckeditor along-with ItextSahrp so whenever user types any5thing and clicks on submit i create a pdf file with the help ItextSharp.

Is there a way to detect whether user has entered arabic word. You are using the below code to detect the arabic words but when i used this and entered English it worked which shouldn't have happened

//Create a regex expression to detect hebrew or arabic code points
const string regex_match_arabic_hebrew = @"[\u0600-\u06FF,\u0590-\u05FF]+";
if (Regex.IsMatch("מה קורה", regex_match_arabic_hebrew, RegexOptions.IgnoreCase))
{
table.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
}

Can you suggest something or any article from where i can get some ideas or help??

Thanks
Sourabh
Left by Sourabh Sharma on May 23, 2012 10:06 PM

# re: Using iTextSharp to correctly display Hebrew / Arabic text (Right to Left) in a PDF Document
Requesting Gravatar...
There is good solution, but i was need solution to convert HTML Hebrew to PDF, after many trying i find this working: solution whit Arabian lang : http://forums.asp.net/t/1650876.aspx/1 . In this code problem only with <table> tags.
and convert it to Hebrew :

private string createPDF(string html)
{
string path = "~/App_Data/Files/p_" + Guid.NewGuid() + ".pdf";
string strHTMLpath = "~/App_Data/Files/test.html";

Document document = new Document(PageSize.A4, 80, 50, 30, 65);
PdfWriter.GetInstance(document, new FileStream(Server.MapPath(path), FileMode.Create));
document.Open();


List<IElement> objects;
document.NewPage();

//var stream = new StreamReader(Server.MapPath(strHTMLpath), Encoding.Default).ReadToEnd();
//objects = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StreamReader(Server.MapPath(strHTMLpath), Encoding.UTF8), new StyleSheet());
objects = HTMLWorker.ParseToList(new StringReader(html), new StyleSheet());

BaseFont bf = BaseFont.CreateFont(Server.MapPath("~/Content/ARIALUNI.TTF"), BaseFont.IDENTITY_H, true);
for (int k = 0; k < objects.Count; k++)
{
PdfPTable table = new PdfPTable(1);
table.RunDirection = PdfWriter.RUN_DIRECTION_RTL;


var els = (IElement)objects[k];
foreach (Chunk el in els.Chunks)
{
#region set persian font
iTextSharp.text.Font f2 = new iTextSharp.text.Font(bf, el.Font.Size,
el.Font.Style, el.Font.Color);
el.Font = f2;
#endregion set persian font


#region Set right to left for persian words
PdfPCell cell = new PdfPCell(new Phrase(10, el.Content, el.Font));
cell.BorderWidth = 0;
table.AddCell(cell);
#endregion Set right to left for persian words
}
//document.Add((IElement)objects[k]);
document.Add(table);
}


document.Close();

return path;
}
Left by Uliana on Apr 14, 2013 10:17 PM

# re: Using iTextSharp to correctly display Hebrew / Arabic text (Right to Left) in a PDF Document
Requesting Gravatar...
Hello,

I have html code from which i create pdf.
All data display properly, only direction is not in right.
I don't use neither pdftable nor pdfcell.
Please help me or suggest some links.

Thanks in advance,
Hiral Shah
Left by hiral on May 15, 2013 5:51 PM

# re: Using iTextSharp to correctly display Hebrew / Arabic text (Right to Left) in a PDF Document
Requesting Gravatar...
not working with arial.ttf
Left by UnKnown on Jul 28, 2013 9:19 PM

# re: Using iTextSharp to correctly display Hebrew / Arabic text (Right to Left) in a PDF Document
Requesting Gravatar...
Nice article..
based on the above sample , I made routine, which was working fine to create PDF file Arabic as well English too.

Last month , I moved OS xp to WIndows 2008.. now when I am running the same, I got an error of Arabic text conversion..It is showing the data as it exist in the Oracle Database.. Our Conversion routine gets fail.. C the Routine for it...


private string ConvertToAsciiString(string str)
{
Encoding source = Encoding.Default ;
Encoding dest = Encoding.Unicode ;
// Convert the string into a byte[].
byte[] sourceBytes = dest.GetBytes(str);
// Perform the conversion from one encoding to the other.
byte[] destBytes = Encoding.Convert(source, dest, sourceBytes);
string asciiString = dest.GetString(destBytes);
asciiString = asciiString.Replace("\0", string.Empty);
return asciiString;
}


Any help will be appreciated..


Left by Nadeem kazmi on Feb 11, 2014 10:40 PM

# re: Using iTextSharp to correctly display Hebrew / Arabic text (Right to Left) in a PDF Document
Requesting Gravatar...
hi Uliana thanks for your example.


did you find any solution for the table tag.


i need a solution
Left by mohammed naji on Dec 19, 2014 6:30 AM

# re: Using iTextSharp to correctly display Hebrew / Arabic text (Right to Left) in a PDF Document
Requesting Gravatar...
bhai ye to text issue sahi nhin ho raha
Left by kapil on Feb 04, 2015 9:41 PM

# re: Using iTextSharp to correctly display Hebrew / Arabic text (Right to Left) in a PDF Document
Requesting Gravatar...
Please need help related to non English data for Arabic .
I have write web service which which get data in Data Table and and then generate PDF and send in email from memory stream.
i am facing the issue that Arabic is not display properly. please help thanks.
Left by Noor on Feb 14, 2016 7:49 PM

Your comment:
 (will show your gravatar)


Copyright © JaydPage | Powered by: GeeksWithBlogs.net