Edit a table with RTL content in C# and .NET

Document management plays a vital role in today's evolving information ecosystem. The need to edit tables containing right-to-left (RTL) text is particularly pressing. In this context, we will examine how to programmatically work with such tables using C# and .NET, as well as the Document.NET from SautinSoft library. This article will help you understand the advantages of this approach, its practical benefits, and its application areas.

Most modern applications require working with multilingual text, a significant portion of which are right-to-left languages ​​such as Arabic, Hebrew, Persian, Urdu, and others. Final documents, reports, presentations, and templates often contain tables containing these languages. Manual editing or automated programmatic modification of RTL tables without proper configuration can result in incorrect display, disruption of the document's structure, or loss of readability. Therefore, it is important to use tools that take into account the specifics of these languages, in addition to simply editing the text.

In practice, the use cases are varied:

  • Automated report generation for international companies using Arabic or Hebrew.
  • Creation of multilingual document templates with tables that require dynamically populating or updating content.
  • Processing client data and generating documents with a multilingual interface.
  • Integration into electronic document management systems that require automatic editing and formatting.

The practical benefits of this approach are enormous. It enables the automation of multilingual document processing, maintaining their structure and readability in various formats, and scaling work with large data volumes. As part of an automated system, such code increases document processing speed, reducing the likelihood of errors associated with manual editing. It is especially relevant for international projects that require generating and editing homogeneous multilingual tables in documents.

Input file:

table of content input

Output result:

table of content output

Complete code

using SautinSoft.Document;
using SautinSoft.Document.Tables;
using System;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;

namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            // Get your free trial key here:   
            // https://sautinsoft.com/start-for-free/

            RTLTable();
        }

        /// <summary>
        /// How to add Rigth-to-Left text in a table.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/right-to-left-table.php
        /// </remarks>
        public static void RTLTable()
        {
            string sourcePath = @"..\..\..\RTL.docx";
            string destPath = "RTL.pdf";
            DocumentCore dc = DocumentCore.Load(sourcePath);
            // Show line numbers on the right side of the page
            var pageSetup = dc.Sections[0].PageSetup;
            pageSetup.LineNumberRestartSetting = LineNumberRestartSetting.Continuous;
          
            // Create a new right-to-left paragraph
            var paragraph = new Paragraph(dc);
            paragraph.ParagraphFormat.RightToLeft = true;
            paragraph.Inlines.Add(new Run(dc, "أخذ عن موالية الإمتعاض"));
           
            dc.Sections[0].Blocks.Add(paragraph);

            // Create a right-to-left table with some data inside. 
            var table = new Table(dc);
           
            table.TableFormat.PreferredWidth = new TableWidth(100, TableWidthUnit.Percentage);
            var row = new TableRow(dc);
            table.TableFormat.Borders.SetBorders(MultipleBorderTypes.Inside | MultipleBorderTypes.Outside, BorderStyle.Single, Color.Black, 1);
            table.Rows.Add(row);

            var firstCellPara = new Paragraph(dc, "של תיבת תרומה מלא");
            firstCellPara.ParagraphFormat.RightToLeft = true;
            row.Cells.Add(new TableCell(dc, firstCellPara));

            var secondCellPara = new Paragraph(dc, "200");
            row.Cells.Add(new TableCell(dc, secondCellPara));
            dc.Sections[0].Blocks.Add(table);

            // Save the document as PDF.
            dc.Save(destPath, new PdfSaveOptions());

            // Show the source and the dest documents.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(sourcePath) { UseShellExecute = true });
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(destPath) { UseShellExecute = true });
        }
    }
}

Download

Option Infer On

Imports SautinSoft.Document
Imports SautinSoft.Document.Tables
Imports System
Imports System.IO
Imports System.Linq
Imports System.Reflection.Metadata

Namespace Sample
	Friend Class Sample
		Shared Sub Main(ByVal args() As String)
			' Get your free trial key here:   
			' https://sautinsoft.com/start-for-free/

			RTLTable()
		End Sub

		''' <summary>
		''' How to add Rigth-to-Left text in a table.
		''' </summary>
		''' <remarks>
		''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/right-to-left-table.php
		''' </remarks>
		Public Shared Sub RTLTable()
			Dim sourcePath As String = "..\..\..\RTL.docx"
			Dim destPath As String = "RTL.pdf"
			Dim dc As DocumentCore = DocumentCore.Load(sourcePath)
			' Show line numbers on the right side of the page
			Dim pageSetup = dc.Sections(0).PageSetup
			pageSetup.LineNumberRestartSetting = LineNumberRestartSetting.Continuous

			' Create a new right-to-left paragraph
			Dim paragraph As New Paragraph(dc)
			paragraph.ParagraphFormat.RightToLeft = True
			paragraph.Inlines.Add(New Run(dc, "أخذ عن موالية الإمتعاض"))

			dc.Sections(0).Blocks.Add(paragraph)

			' Create a right-to-left table with some data inside. 
			Dim table As New Table(dc)

			table.TableFormat.PreferredWidth = New TableWidth(100, TableWidthUnit.Percentage)
			Dim row = New TableRow(dc)
			table.TableFormat.Borders.SetBorders(MultipleBorderTypes.Inside Or MultipleBorderTypes.Outside, BorderStyle.Single, Color.Black, 1)
			table.Rows.Add(row)

			Dim firstCellPara = New Paragraph(dc, "של תיבת תרומה מלא")
			firstCellPara.ParagraphFormat.RightToLeft = True
			row.Cells.Add(New TableCell(dc, firstCellPara))

			Dim secondCellPara = New Paragraph(dc, "200")
			row.Cells.Add(New TableCell(dc, secondCellPara))
			dc.Sections(0).Blocks.Add(table)

			' Save the document as PDF.
			dc.Save(destPath, New PdfSaveOptions())

			' Show the source and the dest documents.
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(sourcePath) With {.UseShellExecute = True})
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(destPath) With {.UseShellExecute = True})
		End Sub
	End Class
End Namespace

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:


Captcha

Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.