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?

Community
  • 1
  • 1
M.S
  • 288
  • 4
  • 12
  • 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 '14 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 '14 at 18:26
  • according to this: http://stackoverflow.com/a/7048709/1707033 you can set it on a element which is added to the document. – HellBaby Nov 17 '14 at 18:31

1 Answers1

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();
           }
       }
   }
}
Iman Hamidi
  • 188
  • 3
  • 6