Articles in this section
Category / Section

How to embed unsupported fonts while exporting WinRT DataGrid to PDF?

5 mins read

SfDataGrid exports data to PDF using Syncfusion PDF component. By default, some fonts (such as Unicode font) are not supported in PDF. In this case, it is possible to embed the font in PDF document with the help of PdfTrueTypeFont.

Here, you can see how to embed font into the PDF document. In SfDataGrid, you can customize the content that is to be exported into PDF, by using delegate handlers in the PdfExportingOptions.

The following code illustrates how to set the event to ExportingEventHandler delegate of PdfExportingOptions where you can embed Font.

C#

var options = new PdfExportingOptions();
//The event is directly assigned to ExportingEventHandler delegate
options.ExportingEventHandler = GridPdfExportingEvent;
 var pdfDocument = this.grid.ExportToPdf(options);
public void GridPdfExportingEvent(object sender, GridPdfExportingEventArgs args)
      {
        }

The following code illustrates how to create a font from the resource file and embed into the font of PdfGridCell cell style in WinRT.

C#

public void GridPdfExportingEvent(object sender, GridPdfExportingEventArgs args)
        {
            //Here we  have load the image for each cell,by Checking if the ExportCellType is RecordCell or not 
            if (args.CellType == ExportCellType.RecordCell)
            {
                //Creates a new font from the font file and save as Stream type
                Stream imagestream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("PDFGeneration_EuroSymbol.Assets.ARIALUNI.TTF") as Stream;
                //creating the true type font
                PdfFont font = new PdfTrueTypeFont(imagestream, 9f, PdfFontStyle.Regular);
                //Assigns that font to PDFGridCell
                args.CellStyle.Font = font;
            }
        }

In Silverlight, you can render the stream by using GetResourceStream() method to retrieve the font from the resource file and embed into the font of PdfGridCell cell style as illustrated in the following code example.

C#

private void GridCellPdfExportingEvent(object sender, GridPdfExportingEventArgs e)
        {
            //Here we  have load the image for each cell,by Checking if the ExportCellType is RecordCell or not 
            if (e.CellType == ExportCellType.RecordCell)
            {
                //Creates a new font from the font file and save as Stream type
                Stream stream = Application.GetResourceStream(new Uri("Sfdatagrid_PdfExportsymbol;component/Assets/ARIALUNI.TTF", UriKind.Relative)).Stream;
                //Creating the true type font
                PdfFont font = new PdfTrueTypeFont(stream, 9f, PdfFontStyle.Regular);
                //Assigns that font to PDFGridCell
                e.CellStyle.Font = font;
            }
        }

In the above code, unicode font embeds into PDF. Therefore, exporting the SfDataGrid with Unicode character is exported to PDF correctly.

SfDataGrid

Figure 1: SfDataGrid

Following is the exported PDF document with Unicode character.

Exported PDF

Figure 2: Exported PDF

Sample link:

You can refer to the following sample links for exporting unsupported fonts from SfDataGrid to PDF document in WPF, WinRT and Silverlight.

WPF: PDFGenerationEuroSymbol_WPF

WRT: PDFGenerationEuroSymbol_WRT

Silverlight: PDFGenerationEuroSymbol_SilverLight

 

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied