probleme with Arabic text in C# using iTextSharp

2.1k views Asked by At

I have a problem with my code which generates an erroneous writing in Arabic.

This is my code

Excel.Workbook MyBook = null;
Excel.Application MyApp = null;
Excel.Worksheet MySheet = null;

MyApp = new Excel.Application();
MyApp.Visible = false;
MyBook = MyApp.Workbooks.Open("C:/Users/ADmin/Desktop/TESTCSHARP.xlsx");
MySheet = (Excel.Worksheet)MyBook.Sheets[1]; // Explicit cast is not required here
int lastRow_A = MySheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row;

iTextSharp.text.Rectangle REC = new iTextSharp.text.Rectangle(936, 454);
Document nouveauDocument = new Document(REC.Rotate());

try
{
    PdfWriter WRITER = PdfWriter.GetInstance(nouveauDocument, new FileStream("fichier_test.pdf", FileMode.Create));

    nouveauDocument.Open();

    try
    {
        PdfContentByte cb = WRITER.DirectContent;

        cb.BeginText();
        try
        {
            int XX = 15;
            cb.SetFontAndSize(BaseFont.CreateFont(), 10);
            float Y = (32 * (float)0.3937008) * 72;
            float X = (XX * (float)0.3937008) * 72;

            string arabname = "مرحبا العالم";

            string ARIALUNI_TFF = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF");
            BaseFont bf = BaseFont.CreateFont(ARIALUNI_TFF, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            iTextSharp.text.Font f = new iTextSharp.text.Font(bf, 12, iTextSharp.text.Font.BOLD);

            cb.SetFontAndSize(f.BaseFont, 10);

            cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, arabname, (float)10.7 * (float)0.3937008 * 72, (float)6.8 * (float)0.3937008 * 72, -90);

            MyBook.Close();
        }

        finally
        {
            cb.EndText();
        }
    }

    finally
    {
        nouveauDocument.Close();

        WRITER.Close();
    }

}

catch (DocumentException de)
{
    Console.WriteLine("error " + de.Message);
}

catch (System.IO.IOException ioe)
{
    Console.WriteLine("error " + ioe.Message);
}

nouveauDocument.Close();

the result of this code in pdf is :

ر ي ن م

inversed and not bound like Arabic writing

and when i try to inverse the string with this code

char[] charArray = arabname.ToCharArray();
                    Array.Reverse(charArray);
                    arabname=charArray.ToString();

I have the same result.

while my need is to have منير linked like that, displayed in a PDF

Please help, Thanks.

2

There are 2 answers

0
Dan Field On

Try setting your font to embedded, changing this lines:

BaseFont bf = BaseFont.CreateFont(ARIALUNI_TFF, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

You'll also have to write this text to a table with RunDirection set to PdfWriter.RUN_DIRECTION_RTL to get the text written RTL. See Reversing Strings in Right To Left (BiDirectional) Languages in iTextSharp for more information.

0
xMx On

I took the same code into Reversing Strings in Right To Left (BiDirectional) Languages in iTextSharp. and I have not had the expected result, worst i have an empty page.

and don't know what is the problem ,