Articles in this section
Category / Section

How to draw right-to-left text in a PDF using C# and VB.NET?

5 mins read

In a right-to-left script, writing starts from the right side of the PDF page and continuous to the left. The most commonly used RTL scripts are Arabic, Hebrew, Persian, and Urdu.

Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can draw right-to-left text in a PDF using C# and VB.NET.

Steps to draw right-to-left text in the PDF programmatically:

  1. Create a new C# console application project. Create new console application in WinForms
  2. Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org. Install nuget packages in WinForms
  3. Include the following namespaces in the Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics

 

  1. Use the following code snippet to draw right-to-left text in the PDF document.

C#

//Create a new PDF document
PdfDocument doc = new PdfDocument();
//Add a page to the document
PdfPage page = doc.Pages.Add();
//Create PDF graphics for the page
PdfGraphics graphics = page.Graphics;
//Create font
PdfFont font = new PdfTrueTypeFont(new Font("Arial", 14), true);
//Set the format for string
PdfStringFormat format = new PdfStringFormat();
//Set the property for RTL text
format.TextDirection = PdfTextDirection.RightToLeft;
//Set the alignment
format.Alignment = PdfTextAlignment.Right;
//Draw string with right alignment
graphics.DrawString("مرحبا بالعالم!", font, PdfBrushes.Black, new RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height), format);
//Save the document
doc.Save("RTLText.pdf");
//Close the document
doc.Close(true);
//This will open the PDF file so, the result will be seen in default PDF viewer
Process.Start("RTLText.pdf");

 

VB.NET

'Create a new PDF document
Dim doc As New PdfDocument()
'Add a page to the document
Dim page As PdfPage = doc.Pages.Add()
'Create PDF graphics for the page
Dim graphics As PdfGraphics = page.Graphics
'Create font
Dim font As PdfFont = New PdfTrueTypeFont(New Font("Arial", 14), True)
'Set the format for string
Dim format As New PdfStringFormat()
'Set the property for RTL text
format.TextDirection = PdfTextDirection.RightToLeft
'Set the alignment
format.Alignment = PdfTextAlignment.Right
'Draw string with right alignment
graphics.DrawString("مرحبا بالعالم!", font, PdfBrushes.Black, New RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height), format)
'Save the document
doc.Save("RTLText.pdf")
'Close the document
doc.Close(True)
'This will open the PDF file so, the result will be seen in default PDF viewer
Process.Start("RTLText.pdf")

 

A complete working sample can be downloaded from RTLTextSample.zip.

By executing the program, you will get the PDF document as follows. Screenshot of output pdf file in WinForms

Take a moment to peruse the documentation, where you can find other options like drawing text and multi-column text, consuming TrueType fonts, Standard fonts, and CJK fonts, and other features like form filling, extract text, images from PDF and protect PDF documents with code examples.

Refer here to explore the rich set of Syncfusion Essential PDF features.

An online sample link to draw right-to-left language text in the document.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (2)
Please  to leave a comment
CG
Caroline Gooding

Hi

I am using version 17.4451.0.39 of syncfusion pdf and trying to get right to left text working but format is not recognised, any ideas on this please?

thank you

PV
Prakash Viswanathan

Hi Caroline,

Thank you for contacting Syncfusion support.

We have checked the reported issue with above KB sample in different formatting on our end, but it is working properly. We have attached the sample with product version (17.4.0.39) for your reference. We request you to share the modified sample, output document, complete code snippet to reproduce the issue on our end. So, that it will be helpful for us to analyze and assist you further on this.

Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/RTLTextSample1844304199

Document: https://www.syncfusion.com/downloads/support/directtrac/general/ze/Document-332996564

Regards,

Gowthamraj K

SA
Samer Alkhatib

It is working for me

Access denied
Access denied