Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RTL alignment in itextsharp c#

Tags:

c#

itext

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?

like image 431
M.S Avatar asked Aug 29 '26 16:08

M.S


1 Answers

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();
           }
       }
   }
}
like image 161
Iman Hamidi Avatar answered Sep 01 '26 06:09

Iman Hamidi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!