Adding RTL Support to Your PDF in Xamarin | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (182).NET Core  (28).NET MAUI  (233)Angular  (117)ASP.NET  (49)ASP.NET Core  (81)ASP.NET MVC  (87)Azure  (42)Black Friday Deal  (1)Blazor  (244)BoldSign  (15)DocIO  (24)Essential JS 2  (110)Essential Studio  (201)File Formats  (74)Flutter  (140)JavaScript  (227)Microsoft  (122)PDF  (86)Python  (1)React  (107)Streamlit  (1)Succinctly series  (131)Syncfusion  (993)TypeScript  (33)Uno Platform  (3)UWP  (3)Vue  (46)Webinar  (53)Windows Forms  (59)WinUI  (72)WPF  (165)Xamarin  (159)XlsIO  (38)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (11)Business intelligence  (55)Button  (4)C#  (164)Chart  (154)Chart of the week  (62)Cloud  (16)Company  (440)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (74)Development  (694)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (7)Essential Tools  (13)Excel  (43)Extensions  (31)File Manager  (8)Gantt  (23)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (530)Mobile MVC  (9)OLAP server  (2)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (48)Performance  (13)PHP  (2)PivotGrid  (5)Predictive Analytics  (6)Report Server  (3)Reporting  (8)Reporting / Back Office  (9)Rich Text Editor  (12)Road Map  (12)Scheduler  (54)Security  (5)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (32)Solution Services  (4)Spreadsheet  (11)SQL  (15)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (426)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (648)What's new  (354)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)

Adding RTL Support to Your PDF in Xamarin

Syncfusion Essential PDF is a .NET PDF library from the Essential Studio 2018 Volume 2 release (16.2) that can be used to draw right-to-left (RTL) and bi-directional (Bidi) text in a PDF document.

Say goodbye to tedious PDF tasks and hello to effortless document processing with Syncfusion's PDF Library.

What is right-to-left support?

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

مرحبا بالعالم

There are also languages which have more than one script and can be written either right-to-left or left-to-right, such as Sindhi or Kurdish.

What is bi-directional support?

Bi-directional text contains both right-to-left and left-to-right formats. For instance, a line may contain both Arabic and English texts. As you might imagine, there are several possibilities when dealing with Bidi text. Arabic text is written from right-to-left, but numbers are generally written left-to-right.

هناك 10 تفاحات

Adding RTL support to your PDF

While this post deals with the overall understanding of right-to-left, left-to-right, and bi-directional text, a little bit of code can help to understand how things works in the PDF.

  1. Create a cross-platform app in Xamarin.Forms.
  2. Select the Blank App template and the .NET Standard option under Code Sharing Strategy.

  3. Add the Syncfusion.Xamarin.PDF NuGet packages as a reference to the .Net Standard project in your Xamarin application.
  4. Since the PDF standard font does not support Unicode character, you will need to add the TrueType font file to the assets folder in the .NET Standard project. Right click the font, select properties, and set its build action as embedded resource.
  5. Add a button in the MainPage.xaml file.
    <?xml version="1.0" encoding="utf-8"?>
    <ContentPage font-weight: bold;">
        <StackLayout Padding="10">
           <Label x:Name="Content_heading" Text="Right-to-left text in PDF" FontSize="Large" FontAttributes="Bold" HorizontalOptions="Center" VerticalOptions="Center"></Label>
           <Label x:Name="Content_1" Text="This sample demonstrates how to add RTL text in the PDF document." FontSize="Medium" VerticalOptions="Center"></Label>
           <Button x:Name="btnGenerate" Text="Generate PDF" HorizontalOptions="Center" VerticalOptions="Center"></Button>
  6. Add the following code with the onButtonClicked method in the MainPage.xaml.cs file.
    //Create a new PDF document.
    PdfDocument document = new PdfDocument();
    //Add a new PDF page.
    PdfPage page = document.Pages.Add();
    //Load font.
    Stream fontStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("RTLDemo.Assets.arial.ttf");
    //Create PDF TrueType font.
    PdfFont pdfFont = new PdfTrueTypeFont(fontStream, 12);
    //String format 
    PdfStringFormat format = new PdfStringFormat();
    //Set the format as right to left.
    format.TextDirection = PdfTextDirection.RightToLeft;
    //Set the alignment.
    format.Alignment = PdfTextAlignment.Right;
    SizeF pageSize = page.GetClientSize();
    page.Graphics.DrawString("مرحبا بالعالم!", pdfFont, PdfBrushes.Black, new Syncfusion.Drawing.RectangleF(0, 0, pageSize.Width, pageSize.Height), format);
    MemoryStream ms = new MemoryStream();
    //Save the document.
    //Close the document 
    ms.Position = 0;
    if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
        Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("RTLText.pdf", "application/pdf", ms);
        Xamarin.Forms.DependencyService.Get<ISave>().Save("RTLText.pdf", "application/pdf", ms);

GitHub reference

You can find the sample for adding RTL text to PDF in the GitHub repository.

Join thousands of developers who rely on Syncfusion for their PDF needs. Experience the difference today!

Wrapping up

Your application now supports generating a PDF with RTL and Bidi texts using Essential PDF. Take a moment to peruse the documentation, where you’ll find other options and features, all with accompanying code examples.

If you are new to our PDF library, it is highly recommended that you follow our Getting Started guide.

If you’re already a Syncfusion user, you can download the product setup here. If you’re not yet a Syncfusion user, you can download a free, 30-day trial here.

If you have any questions or require clarifications about these features, please let us know in the comments below. You can also contact us through our support forum or Direct-Trac. We are happy to assist you!

If you like this blog post, we think you’ll also like the following free e-books:


Share this post:

Comments (3)

and this is how to set that for DataGridPdfExport:

DataGridPdfExportingController pdfExport = new DataGridPdfExportingController();
MemoryStream stream = new MemoryStream();
PdfDocument pdfDocument = new PdfDocument();
var pdfDoc = new PdfDocument();
PdfPage page = pdfDoc.Pages.Add();
var exportToPdfGrid = pdfExport.ExportToPdfGrid(datagridInvoices,
datagridInvoices.View, new DataGridPdfExportOption()
FitAllColumnsInOnePage = true,

}, pdfDoc);
PdfLayoutFormat layoutFormat = new PdfLayoutFormat();
//exportToPdfGrid Cell Style
Syncfusion.Pdf.Grid.PdfGridCellStyle pdfgs = new Syncfusion.Pdf.Grid.PdfGridCellStyle();
//Load font.
Stream fontStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream(“MBxAPP.Portable.Assets.IRANSansMonoSpacedNum.ttf”);
//Create PDF TrueType font.
PdfFont pdfFont = new PdfTrueTypeFont(fontStream, 12);
//set exportToPdfGridCellStyle Font
pdfgs.Font = pdfFont;
//define PdfStringFormat
PdfStringFormat textFormat = new PdfStringFormat();
//Set the format as right to left.
textFormat.TextDirection = PdfTextDirection.RightToLeft;

//Set the alignment.
textFormat.Alignment = PdfTextAlignment.Right;
//Set data cells Format and Style
for (int i = 0; i < exportToPdfGrid.Rows.Count; i++)
for (int j = 0; j < exportToPdfGrid.Columns.Count; j++)
exportToPdfGrid.Rows[i].Cells[j].StringFormat = textFormat;
exportToPdfGrid.Rows[i].Cells[j].Style = pdfgs;
//Set header cells Format and Style
for (int i = 0; i < exportToPdfGrid.Headers.Count; i++)
exportToPdfGrid.Headers[i].Cells[0].StringFormat = textFormat;
exportToPdfGrid.Headers[i].Cells[0].Style = pdfgs;
exportToPdfGrid.Draw(page, new PointF(10, 10), layoutFormat);

Update :
Edit these lines to fix header Problem
//Set header cells Format and Style
for (int i = 0; i < exportToPdfGrid.Columns.Count; i++)
exportToPdfGrid.Headers[0].Cells[i].StringFormat = textFormat;
exportToPdfGrid.Headers[0].Cells[i].Style = pdfgs;


Thanks for your update.

Your solution is the one of the way to provide the RTL support for cells when exporting datagrid to PDF. But, you can reduce the exporting time by customizing properties of each PdfCell when exporting it using CellExporting event. This will avoid re-iteration of the rows and columns after exporting datagrid to PdfGrid. The following KB illustrates the same,

In the above KB, we have documented about retaining the middle eastern language. You can also set the necessary styling for PdfCell.

More reference links

Chinnu M

Comments are closed.

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed