3

I want to add Arabic text to my pdf. I need to do an RTL alignment. Hebrew text in PDF gives the answer how to do using PdfpTable.

And using

table.RunDirection = PdfWriter.RUN_DIRECTION_RTL;

I can do that. But is there any other way to do so, when I am not creating a PdfPTable at all?

3
  • I would sugest you not using itextsharp. I would suggest you using wkhtmltopdf in which you can create a html to look as you want and to use that one; any setting you will do in your html(including css) will be translated to pdf. That's just my opinion. We used itextsharp and dumped it cause of it's limitations.
    – HellBaby
    Nov 17, 2014 at 18:21
  • @HellBaby Thanks. I will definitely consider your suggestion. But for now i need to know this one. it would be nice if you can help.
    – M.S
    Nov 17, 2014 at 18:26
  • according to this: stackoverflow.com/a/7048709/1707033 you can set it on a element which is added to the document.
    – HellBaby
    Nov 17, 2014 at 18:31

1 Answer 1

5

You can use ColumnText and set RunDirection of it to RUN_DIRECTION_RTL:

using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace iTextSharpTests
{
   class Program
   {
       static void Main(string[] args)
       {
           using (var pdfDoc = new Document(PageSize.A4))
           {
               var pdfWriter = PdfWriter.GetInstance(pdfDoc, new FileStream("Test.pdf", FileMode.Create));
               pdfDoc.Open();

               var fontPath = Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\tahoma.ttf";
               var baseFont = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
               var tahomaFont = new Font(baseFont, 10, Font.NORMAL, BaseColor.BLACK);

               ColumnText ct = new ColumnText(pdfWriter.DirectContent);
               ct.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
               ct.SetSimpleColumn(100, 100, 500, 800, 24, Element.ALIGN_RIGHT);

               var chunk = new Chunk("تست", tahomaFont);

               ct.AddElement(chunk);
               ct.Go();
           }
       }
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.