How to export Arabic letter to PDF in Blazor DataGrid?

Answer:

To export Arabic or any other language letters(True type font), set True Type font information as base64 string to FontFamily of PdfGridFont. There is support for PdfTrueTypeFont in PdfExport method. Here is the code snippet for reference.

<SfGrid DataSource="@list" ID="grid" Toolbar="@(new List<string>() { "PdfExport" })" AllowPdfExport="true" @ref="grid">

<GridEvents OnToolbarClick="ToolbarClick" TValue="EmployeeData">GridEvents>

...

SfGrid>

public async Task ToolbarClick(Syncfusion.Blazor.Navigations.ClickEventArgs args)

{

PdfExportProperties ExportProperties = new PdfExportProperties();

PdfTheme Theme = new PdfTheme();

PdfThemeStyle HeaderThemeStyle = new PdfThemeStyle()

{

Font = new PdfGridFont { IsTrueType = true, FontSize = 8, FontFamily = "T1RUTwA..." }, //apply your custom font base64 string to FontFamily

FontColor = "#64FA50",

FontName = "Calibri",

FontSize = 17,

Bold = true,

Border = HeaderBorder

};

Theme.Header = HeaderThemeStyle;

PdfThemeStyle RecordThemeStyle = new PdfThemeStyle()

{

Font = new PdfGridFont { IsTrueType = true, FontSize = 8, FontFamily = "T1RUTwALAI..." }, //apply your custom font base64 string to FontFamily

FontColor = "#64FA50",

FontName = "Calibri",

FontSize = 17

};

Theme.Record = RecordThemeStyle;

PdfThemeStyle CaptionThemeStyle = new PdfThemeStyle()

{

Font = new PdfGridFont { IsTrueType = true, FontSize = 8, FontFamily = "T1RUT...ABQ=" }, //apply your custom font base64 string to FontFamily

FontColor = "#64FA50",

FontName = "Calibri",

FontSize = 17,

Bold = true

};

Theme.Caption = CaptionThemeStyle;

ExportProperties.Theme = Theme;

await this.grid.PdfExport(ExportProperties);

}


To use the custom font base64 string in an application. Follow the below steps to get the base64 string for the custom font.

  • Download TTF file for the corresponding (custom) font.
  • Then convert the TTF file into base64 string (https://www.giftofspeed.com/base64-encoder/).
  • Finally apply the base64 string to pdfTrupetypeFont.


5 Replies

AB abdoessalam December 7, 2021 09:25 AM UTC

Hi, 

Thank you for the help.

The font problem is partially solved as 

محمد

will be presented as 

د م ح م

i.e the letters are in reverse order without any concatenation 


Best regards,

AM



VN Vignesh Natarajan Syncfusion Team December 8, 2021 03:30 AM UTC

Hi Abdoessalam, 
 
Thanks for the update.  
 
Query: “the letters are in reverse order without any concatenation  
 
We have analyzed your query and we would like to inform you that text displayed in Pdf file will be based on the Family Font TTF file downloaded. So kindly share more details about the family font you have downloaded and referred in the application. Also share the following detail.  
 
  1. Share the Grid code example along with exported file.
 
It will be very helpful for us to validate the reported query at our end and provide solution as early as possible.   
 
Regards, 
Vignesh Natarajan 




MA Mahdi September 11, 2022 05:47 AM UTC

I found solution: (i am using blazor server side package v20.2.0.48)

embed your fonts as Resource in your project you can use any true type font that support right-to-left languages


then code:

PdfExportProperties Props = new PdfExportProperties();
Props.BeginCellLayout = new PdfGridBeginCellLayoutEventHandler(BeginCellEvent);
Grid.ExportToPdfAsync(Props);

and this:

MemoryStream Stream = new MemoryStream(Properties.Resources.XBZar);
private void BeginCellEvent(object sender, PdfGridBeginCellLayoutEventArgs args)
{
args.Style = new PdfGridCellStyle()
{
Font = new PdfTrueTypeFont(Stream, 10, PdfFontStyle.Regular),
StringFormat = new PdfStringFormat()
{
TextDirection = PdfTextDirection.RightToLeft
}
};
}


done!



SP Sarveswaran Palani Syncfusion Team September 12, 2022 06:08 PM UTC

Hi Mahdi,


Thanks for reaching Syncfusion support again.


We appreciate you for taking time to informing us.


Please get back to us if you have any further queries.


Regards,

Sarveswaran PK



VN Vignesh Natarajan Syncfusion Team February 2, 2023 07:24 AM UTC

Hi Meysam,


Thank you for reaching out to us with your question regarding embedding a font as a resource in your project.


Mahdi has provided a solution that involves storing the font in a resource file and accessing it using a MemoryStream. Then font is then used in the BeginCellLayout property of the PdfExportProperties.


If you are encountering any issues with this approach, please share the following details with us:


  1. Your code example
  2. Your Syncfusion NuGet package version
  3. A detailed description of your exact issue or requirement


This information will be extremely helpful in resolving your issue as quickly and efficiently as possible.


Please don't hesitate to reach out to us if you have any further questions or concerns. We are here to help.


Best regards,

Vignesh Natarajan



Loader.
Up arrow icon