Arabic Text not rendering in proper format

hi 


We are facing issue in Xamarin forms , while creating PDF using SfPDF, but Arabic text rendering as wrong (it letter are not joining and right to left). 

Output Screen shot attached

Syncfusion  19.04.38





 PdfDocument document = new PdfDocument();


            //Add a page to the document

            PdfPage page = document.Pages.Add();


            //Create PDF graphics for the page

            PdfGraphics graphics = page.Graphics;


            //Set the standard font

            //PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);


            Stream fontStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("sample.Asset.dubai.ttf");

            Stream fontStream1 = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("sample.Asset.arial.ttf");

            //Initialize the PDF TrueType font.


            PdfFont font = new PdfTrueTypeFont(fontStream, 25);

            PdfFont font1 = new PdfTrueTypeFont(fontStream1, 25,PdfFontStyle.Regular);

            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);



            //Draw the text

            graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));


            //graphics.DrawString("Hello World!!!" + "كمية الذهب", font, PdfBrushes.Black, new PointF(20, 20));

            //graphics.DrawString("كمية الذهب", font, PdfBrushes.Black, new PointF(40, 40));

            //graphics.DrawString("كمية الذهب", font1, PdfBrushes.Black, new PointF(60, 60));


            page.Graphics.DrawString("مرحبا عربي للتجربة", font, PdfBrushes.Black, new RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height), format);


            //Save the document to the stream

            MemoryStream stream = new MemoryStream();

            document.Save(stream);


            pdfViewerControl.LoadDocument(stream);


            //Close the document

            document.Close(true);




Attachment: scr_66be9d61.zip

4 Replies

SV Surya Venkatesan Syncfusion Team January 24, 2022 04:52 PM UTC

Hi saijan,


We recommend you drawString with the stringFormat of its textDirection API to set the rightToleft or leftToright direction accordingly and also enable the complexScript to get better results of Arabic text. Kindly try the following code snippet and let us know the result,

            PdfDocument document = new PdfDocument();

            //Add a page to the document

            PdfPage page = document.Pages.Add();

            //Create PDF graphics for the page

            PdfGraphics graphics = page.Graphics;

 

            Stream fontStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("sample.Asset.dubai.ttf");

 

            Stream fontStream1 = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("sample.Asset.arial.ttf");

 

            //Initialize the PDF TrueType font.

            PdfFont font = new PdfTrueTypeFont(fontStream, 25);

 

            PdfStringFormat format = new PdfStringFormat();

            format.TextDirection = PdfTextDirection.RightToLeft;

            format.ComplexScript = true;

           

            //Draw the text

            graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));

 

            page.Graphics.DrawString("مرحبا عربي للتجربة", font, PdfBrushes.Black, new RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height), format);

 

            //Save the document to the stream

            MemoryStream stream = new MemoryStream();

            document.Save(stream);


If the above code snippet does not meet your requirement, Kindly revert us with the further details of your requirement.


Regards,

Surya V



AA Ajimon Anirudhan April 28, 2023 02:23 PM UTC

HI  Surya Venkatesan

I have copied my font in Asset folder

Stream fontStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("pdffont.ttf");


The above code is giving null result.


Please help to read this font from asset folder



GK Gowthamraj Kumar Syncfusion Team May 1, 2023 01:18 PM UTC

We can draw the Arabic text with right-to-left (RTL) format in a PDF document in Xamarin Platform. We have to embed the font files in Xamarin Forms application by setting the build action to embedded resource. Please follow the below steps in your end and let us know if it is suites your requirement or not,


  • Include the supported font file into the Assets folder.
  • Right click the font file and Go to properties and Set Build Action to Embedded resource and copy always


Please find the below screenshot,



Note: Please make sure the using font file should supports the Arabic text or not. We have to use the supported font file to preserve the text properly.


Please refer the below link for more information,

https://www.syncfusion.com/blogs/post/adding-rtl-support-to-your-pdf-in-xamarin.aspx

https://support.syncfusion.com/kb/article/8560/how-to-draw-unicode-characters-in-a-pdf-in-xamarin

We have attached the simple sample to draw a Arabic and English text with output document for your reference, please try the sample on your end and let us know the result.



Attachment: Output_f18d4629.pdf


GK Gowthamraj Kumar Syncfusion Team May 1, 2023 01:19 PM UTC

sample attachment


Attachment: CreatePDFwithArabixTextSample_10d03158.zip

Loader.
Up arrow icon