Articles in this section
Category / Section

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

6 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

 

 

Conclusion

I hope you enjoyed learning about how to embed unsupported fonts while exporting DataGrid to PDF.


You can refer to our WPF DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF DataGrid example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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