M.S
M.S

Reputation: 298

RTL alignment in itextsharp c#

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?

Upvotes: 3

Views: 3029

Answers (1)

Iman Hamidi
Iman Hamidi

Reputation: 198

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

Upvotes: 5

Related Questions