How to draw right-to-left text in a PDF using C# and VB.NET?
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:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- 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
- 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.
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.
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.
It is working for me
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