How to create Unicode Font with PdfTrueTypeFont or the proper way to draw a unicode string??

Hello,
i'm trying to follow the documentation on how to draw text when creating a PDF here:
https://help.syncfusion.com/file-formats/pdf/working-with-xamarin
all works well for english text, now i want to print unicode text but the PDF is empty, so i searched more and found this:
https://help.syncfusion.com/file-formats/pdf/working-with-text
but while trying to createPdfTrueTypeFont object following the documentation, it doesnt have a constructor that takes the same parameters posted in documentation:
PdfFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 14), true);
^ PdfTrueTypeFont class doesnt have a constructor that take Font object
Not sure if this documentation is meant for xamarin or not.
whats the proper way to draw unicode text when generating a PDF?
Thanks

6 Replies

SL Sowmiya Loganathan Syncfusion Team February 16, 2018 06:10 AM UTC

Hi Yazeed, 

Thank you for contacting Syncfusion Support. 

We can draw the Unicode text (like € $ ¥ ) by using PdfTrueTypeFont in Xamarin Forms platform.  

Please find the below code snippet for more details :  
            //Load the true type font.  
            Stream fontstream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("App1.Assets.arial.ttf"); 
 
            //Create a new PDF document.  
            PdfDocument doc = new PdfDocument(); 
 
            //Add a new page.  
            PdfPage page = doc.Pages.Add(); 
 
            //Initialize the PDF true type font.  
            PdfFont font = new PdfTrueTypeFont(fontstream, 12, PdfFontStyle.Regular); 
 
            //Draw text.  
            page.Graphics.DrawString("hello world! @ # €", font, PdfBrushes.Black, new Syncfusion.Drawing.PointF(0, 0));            

We have created the sample to add the Unicode character in PDF Grid. Please find the sample from below: 

Please find the below discussion forum link for your reference, 

Kindly try this in your end and check whether your requirement is achieved or not. Please let us know if you need further assistance on this. 

Regards, 
Sowmiya L 



YH Yazeed Hamdan February 18, 2018 07:41 AM UTC

Thank you for your reply, I tried the code snippet and can see the results but characters are reversed, to shed some light on what i'm trying to do, I'm trying to draw Arabic string (RTL), I cannot find "RightToLeft" Property inside the PdfStringFormat object, in the documentation, it shows this:

PdfStringFormat format = new PdfStringFormat();
//Set the format as RTL type.
format.RightToLeft = true;

so here is my code:

PdfFont font = new PdfTrueTypeFont(fontstream, 12, PdfFontStyle.Regular);                       
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);                       
page.Graphics.DrawString("مرحبا عربي للتجربة", font, PdfBrushes.Black, new RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height), format);




SL Sowmiya Loganathan Syncfusion Team February 19, 2018 06:04 AM UTC

 Hi Yazeed,  
  
 
Currently we do not have support to “Draw RTL text to PDF document in Xamarin Forms”. We consider to implement in our next of Volume release. 
 
Please find the our support list in the below link:   
  
Please let us know if you have any further assistance on this.  
 
Regards,  
Sowmiya L  



SK Sasi Kumar Sekar Syncfusion Team June 26, 2018 07:29 AM UTC

Hi Yazeed, 
  
We are glad to announce that our Essential Studio 2018 Volume 2 Release v16.2.0.41 is rolled out and is available for download under the following link. 
 
 
We have implemented the feature with “Need to add support for Draw RTL text in Xamarin Platform” in this release. Please refer the below code snippet for further details. 
 
           //Create a new PDF document.  
            PdfDocument document = new PdfDocument(); 
            //Add a new PDF page. 
            PdfPage page = document.Pages.Add();  
            //Create new PdfTrueTypeFont instance. 
            //Load the font from assets. 
            Stream fontStream = typeof(App).GetTypeInfo(). 
            Assembly.GetManifestResourceStream 
            ("RTLDemo.Assets.arial.ttf"); PdfTrueTypeFont font = new PdfTrueTypeFont(fontStream, 12); 
            //Create a PdfStringFormat. 
            PdfStringFormat format = new PdfStringFormat(); 
            //Set text direction.  
            format.TextDirection = PdfTextDirection.RightToLeft; 
            //Set text alignment.  
            format.Alignment = PdfTextAlignment.Right; 
            //Draw text to the PDF document. 
            page.Graphics.DrawString("بالعالم مرحبا !",font, PdfBrushes.Black, Syncfusion.Drawing.PointF.Empty, format); 
            //Save document. 
            MemoryStream ms = new MemoryStream(); 
            document.Save(ms); 
            //Close the PdfDocument instance. 
            document.Close(true); 
 
 
We thank you for your support and appreciate your patience in waiting for this release. Please get in touch with us if you would require any further assistance. 
 
Regards,            
Sasi Kumar S. 



SH Shreyans May 17, 2019 08:10 AM UTC

Hello,

I have used PDFGrid to display english and arabic text in column and row text. After the PDF file is generated, only english text is shown in PDF, arabic Text is not shown. Below is the code snippet and attached is the output PDF file :

string text = "Hello world!!! Hello world!!!"; 

string Date = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss"); 

string ReceiptTitle = "Receipt for Payment of P.O.Box Rent"; 

string Branch = "Branch : Ruwi"; 

//Creates a new PDF document. PdfDocument doc = new PdfDocument(); //Adds a page. PdfPage page = doc.Pages.Add(); //Acquires page's graphics PdfGraphics graphics = page.Graphics; PdfStringFormat drawFormat = new PdfStringFormat(); drawFormat.WordWrap = PdfWordWrapType.Word; drawFormat.Alignment = PdfTextAlignment.Justify; drawFormat.LineAlignment = PdfVerticalAlignment.Top; //Set the font. PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 10f); //Create a solid brush. PdfBrush brush = new PdfSolidBrush(new PdfColor(Syncfusion.Drawing.Color.Black)); RectangleF bounds = new RectangleF(new PointF(10, 10), new SizeF(page.Graphics.ClientSize.Width - 30, page.Graphics.ClientSize.Height - 20)); // Draw the string one after another. graphics.DrawString("??????? ????? !", font, brush, bounds, drawFormat); graphics.DrawString(Date, font, brush, new PointF(20, 10), drawFormat); graphics.DrawString(ReceiptTitle, font, brush, new PointF(100, 10), drawFormat); graphics.DrawString(Branch, font, brush, new PointF(320, 10), drawFormat); //Create a PdfGrid. PdfGrid pdfGrid = new PdfGrid(); //Add values to list List<object> data1 = new List<object> Object row1 = new { English_Title = "Receipt No.", Value = "001100000008412", Arabic_Title = "??????? ????? !" }; Object row2 = new { English_Title = "Subscriber's Name", Value = "Priya test", Arabic_Title = "??????? ????? !" }; Object row3 = new { English_Title = "Post Box No", Value = "1026", Arabic_Title = "??????? ????? !" }; data1.Add(row1); data1.Add(row2); data1.Add(row3); //Add list to IEnumerable IEnumerable<object> dataTable = data1; //Assign data source. pdfGrid.DataSource = dataTable; for (int i = 0; i < data1.Count; i++) { for (int j = 0; j < 3; j++) { pdfGrid.Rows[i].Cells[j].Style.Borders.All = PdfPens.Transparent; } } //Draws PdfLightTable and returns the rendered bounds. PdfLayoutResult result1 = pdfGrid.Draw(page, new PointF(10, 40)); //draw string with returned bounds from table graphics.DrawString(text, font, brush, result1.Bounds.X, result1.Bounds.Bottom, drawFormat); //Save the PDF document to stream. MemoryStream stream = new MemoryStream(); doc.Save(stream); 

//Close the document. 

doc.Close(true); 

//Save the stream as a file in the device and invoke it for viewing 

Xamarin.Forms.DependencyService.Get().SaveAndView("Output.pdf", "application/pdf", stream);


Attachment: Output.pdf_5ddaf9c0.zip


SL Sowmiya Loganathan Syncfusion Team May 20, 2019 11:00 AM UTC

Hi Yazeed, 

The PDF standard font does not support Unicode characters (Arabic). So, we need to use the PdfTrueTypeFont to preserve the Unicode characters in .NET standard project. Please refer the below code snippet for creating PdfTrueTypeFont instance, here the font is embedded in the project. 

//Load font 
Stream fontStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.arial.ttf"); 
PdfTrueTypeFont font = new PdfTrueTypeFont(fontStream, 12); 

In order to preserve Arabic text in PdfGrid cells, kindly set the font to PdfGridCell using PdfGridCellStyle and set the string format text direction as Right to Left . Please refer the below code snippet, 

PdfGridCellStyle style = new PdfGridCellStyle(); 
style.Font = font; 
style.StringFormat.TextDirection = PdfTextDirection.RightToLeft; 
//Set style to grid  
pdfGridCell.Style = style; 

Refer the below links for more details, 

Please try the above solution in your end and let us know if it solves the issue. 

Regards, 
Sowmiya L 


Loader.
Up arrow icon