Right To Left Support

This example demonstrate Right-to-Left text align support in OfficeComponentPdf.

In todays world there are many languages written not according to usual fashion as left to right.

Our library allows support also for languages written right to left

Clicking generate button will launch a pdf which has several forms of text alignment

Top Center- Rigft to left when text is top center align

Top Right - Right to left when text is top right align

Top Left - Right to left when text is top left align

Middle and justified - Right to left when text is middle and justified left align

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using OfficeComponent.Pdf;
using OfficeComponent.Pdf.Actions;
using OfficeComponent.Pdf.Annotations;
using OfficeComponent.Pdf.Forms;
using OfficeComponent.Pdf.Graphics;
using OfficeComponent.Pdf.Lists;

namespace OfficeComponent.Samples
{
    class RightToLeftSupportExample : ExampleBase
    {
        public RightToLeftSupportExample(string commonDataPath, string outputDir)
            : base(commonDataPath, outputDir)
        {
        }

        public RightToLeftSupportExample(string commonDataPath, string outputDir, string xmlFile) : base(commonDataPath, outputDir, xmlFile)
        {
        }

        public override string Execute()
        {
            // Create a new instance of PdfDocument class.
            PdfDocument document = new PdfDocument();
            PdfPage page = document.Pages.Add();

            // Create font
            Font f = new Font("Tahoma", 14);
            PdfFont font = new PdfTrueTypeFont(f, true);
            string expath = CommonDataPath + "\\Arabic.txt";

            // Read the text from text file
            StreamReader reader = new StreamReader(expath, Encoding.Unicode);
            string text = reader.ReadToEnd();
            reader.Close();

            // Create a brush
            PdfBrush brush = new PdfSolidBrush(Color.Black);
            PdfPen pen = new PdfPen(Color.Black);
            SizeF clipBounds = page.Graphics.ClientSize;
            RectangleF rect = new RectangleF(0, 0, clipBounds.Width, clipBounds.Height / 4f);

            // Set the property for RTL text
            PdfStringFormat format = new PdfStringFormat();
            format.RightToLeft = true;
            format.ParagraphIndent = 35f;

            page.Graphics.DrawString(text, font, brush, rect, format);
            page.Graphics.DrawRectangle(pen, rect);
            page.Graphics.DrawString("Top Left", font, brush, new PointF(rect.Left + 5, rect.Bottom - 25));

            // Set the alignment
            format.Alignment = PdfTextAlignment.Center;
            rect.Offset(0, rect.Height);
            page.Graphics.DrawString(text, font, brush, rect, format);
            page.Graphics.DrawRectangle(pen, rect);
            page.Graphics.DrawString("Top Center", font, brush, new PointF(rect.Left + 5, rect.Bottom - 25));

            format.Alignment = PdfTextAlignment.Right;
            rect.Offset(0, rect.Height);
            page.Graphics.DrawString(text, font, brush, rect, format);
            page.Graphics.DrawRectangle(pen, rect);
            page.Graphics.DrawString("Top Right", font, brush, new PointF(rect.Left + 5, rect.Bottom - 25));

            format.Alignment = PdfTextAlignment.Justify;
            format.LineAlignment = PdfVerticalAlignment.Middle;
            rect.Offset(0, rect.Height);
            page.Graphics.DrawString(text, font, brush, rect, format);
            page.Graphics.DrawRectangle(pen, rect);
            page.Graphics.DrawString("Middle and Justified", font, brush, new PointF(rect.Left + 5, rect.Bottom - 25));

            // Save and close the document.
            var outputPath = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + ".pdf");
            document.Save(outputPath);
            document.Close(true);

            return outputPath;
        }
    }
}
Imports System.IO
Imports System.Text
Imports OfficeComponent.Pdf
Imports OfficeComponent.Pdf.Actions
Imports OfficeComponent.Pdf.Annotations
Imports OfficeComponent.Pdf.Forms
Imports OfficeComponent.Pdf.Graphics
Imports OfficeComponent.Pdf.Lists

Namespace OfficeComponent.Samples
	Friend Class RightToLeftSupportExample
		Inherits ExampleBase
		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
			MyBase.New(commonDataPath, outputDir)
		End Sub

		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
			MyBase.New(commonDataPath, outputDir, xmlFile)
		End Sub

		Public Overrides Function Execute() As String
			' Create a new instance of PdfDocument class.
			Dim document As New PdfDocument()
			Dim page As PdfPage = document.Pages.Add()

			' Create font
			Dim f As New Font("Tahoma", 14)
			Dim font As PdfFont = New PdfTrueTypeFont(f, True)
			Dim expath As String = CommonDataPath & "\Arabic.txt"

			' Read the text from text file
			Dim reader As New StreamReader(expath, Encoding.Unicode)
			Dim text As String = reader.ReadToEnd()
			reader.Close()

			' Create a brush
			Dim brush As PdfBrush = New PdfSolidBrush(Color.Black)
			Dim pen As New PdfPen(Color.Black)
			Dim clipBounds As SizeF = page.Graphics.ClientSize
			Dim rect As New RectangleF(0, 0, clipBounds.Width, clipBounds.Height / 4F)

			' Set the property for RTL text
			Dim format As New PdfStringFormat()
			format.RightToLeft = True
			format.ParagraphIndent = 35F

			page.Graphics.DrawString(text, font, brush, rect, format)
			page.Graphics.DrawRectangle(pen, rect)
			page.Graphics.DrawString("Top Left", font, brush, New PointF(rect.Left + 5, rect.Bottom - 25))

			' Set the alignment
			format.Alignment = PdfTextAlignment.Center
			rect.Offset(0, rect.Height)
			page.Graphics.DrawString(text, font, brush, rect, format)
			page.Graphics.DrawRectangle(pen, rect)
			page.Graphics.DrawString("Top Center", font, brush, New PointF(rect.Left + 5, rect.Bottom - 25))

			format.Alignment = PdfTextAlignment.Right
			rect.Offset(0, rect.Height)
			page.Graphics.DrawString(text, font, brush, rect, format)
			page.Graphics.DrawRectangle(pen, rect)
			page.Graphics.DrawString("Top Right", font, brush, New PointF(rect.Left + 5, rect.Bottom - 25))

			format.Alignment = PdfTextAlignment.Justify
			format.LineAlignment = PdfVerticalAlignment.Middle
			rect.Offset(0, rect.Height)
			page.Graphics.DrawString(text, font, brush, rect, format)
			page.Graphics.DrawRectangle(pen, rect)
			page.Graphics.DrawString("Middle and Justified", font, brush, New PointF(rect.Left + 5, rect.Bottom - 25))

			' Save and close the document.
			Dim outputPath = Path.Combine(OutputDir, Me.GetType().Name & "_" & Guid.NewGuid().ToString() & ".pdf")
			document.Save(outputPath)
			document.Close(True)

			Return outputPath
		End Function
	End Class
End Namespace