| layout | post |
|---|---|
| title | Export To PDF in WPF DataGrid control | Syncfusion® |
| description | Learn here all about Export To PDF support in Syncfusion® WPF DataGrid (SfDataGrid) control and more. |
| platform | wpf |
| control | SfDataGrid |
| documentation | ug |
DataGrid provides support to export data to PDF file. It also provides support for grouping, filtering, sorting, paging, unbound rows, merged cells, stacked headers and details View while exporting.
The following assemblies needs to be added for exporting to PDF file.
- Syncfusion.SfGridConverter.WPF
- Syncfusion.Pdf.Base
For NuGet package, have to install Syncfusion.DataGridExcelExport.WPF package. For more details refer this UG link.
You can export SfDataGrid to PDF by using the following extension methods present in the Syncfusion.UI.Xaml.Grid.Converter namespace.
{% tabs %} {% highlight c# %} using Syncfusion.UI.Xaml.Grid.Converter; var document = dataGrid.ExportToPdf(); document.Save("Sample.pdf"); {% endhighlight %} {% endtabs %}
N> SfDataGrid exports data to PDF file by using Essential PDF. You can refer PDF documentation for manipulating.
Exporting operation can be customized by passing PdfExportingOptions instance as argument to ExportToPdf and ExportToPdfGrid method.
You can export SfDataGrid to PDF by fitting column widths based on its content by setting AutoColumnWidth property as true.
{% tabs %}
{% highlight c# %}
PdfExportingOptions options = new PdfExportingOptions();
options.AutoColumnWidth = true;
var document = dataGrid.ExportToPdf(options);
document.Save("Sample.pdf");
{% endhighlight %}
{% endtabs %}
You can export SfDataGrid to PDF by fitting row heights based on its content by setting AutoRowHeight property as true.
{% tabs %}
{% highlight c# %}
PdfExportingOptions options = new PdfExportingOptions();
options.AutoRowHeight = true;
var document = dataGrid.ExportToPdf(options);
document.Save("Sample.pdf");
{% endhighlight %}
{% endtabs %}
By default, all the columns (including hidden columns) in SfDataGrid will be exported to PDF. If you want to exclude some columns while exporting to PDF, you can use ExcludeColumns property in PdfExportingOptions.
{% tabs %}
{% highlight c# %}
PdfExportingOptions options = new PdfExportingOptions();
options.ExcludeColumns.Add("CustomerName");
options.ExcludeColumns.Add("Country");
var document = dataGrid.ExportToPdf(options);
document.Save("Sample.pdf");
{% endhighlight %}
{% endtabs %}
Here, the columns having CustomerName and Country as MappingName are excluded while exporting.
By default, display text only will be exported to PDF. If you want to export the actual value, you need to set ExportFormat property as false.
{% tabs %}
{% highlight c# %}
PdfExportingOptions options = new PdfExportingOptions();
options.ExportFormat = false;
var document = dataGrid.ExportToPdf(options);
document.Save("Sample.pdf");
{% endhighlight %}
{% endtabs %}
Column headers can be exported on each page by setting RepeatHeaders property.
{% tabs %}
{% highlight c# %}
PdfExportingOptions options = new PdfExportingOptions();
options.RepeatHeaders = true;
var document = dataGrid.ExportToPdf(options);
document.Save("Sample.pdf");
{% endhighlight %}
{% endtabs %}
While exporting to PDF, you can fit all columns on one page by setting FitAllColumnsInOnePage property as true.
{% tabs %}
{% highlight c# %}
PdfExportingOptions options = new PdfExportingOptions();
options.FitAllColumnsInOnePage = true;
var document = dataGrid.ExportToPdf(options);
document.Save("Sample.pdf");
{% endhighlight %}
{% endtabs %}
While exporting data to PDF, if paging is used, current page only will be exported, by default. If you want to export all pages, you need to set ExportAllPages property as true.
{% tabs %}
{% highlight c# %}
PdfExportingOptions options = new PdfExportingOptions();
options.ExportAllPages = true;
var document = dataGrid.ExportToPdf(options);
document.Save("Sample.pdf");
{% endhighlight %}
{% endtabs %}
By default, all the groups in dataGrid will be exported to PDF. If you want to export without Groups, you need to set ExportGroups property as false.
{% tabs %}
{% highlight c# %}
PdfExportingOptions options = new PdfExportingOptions();
options.ExportGroups = false;
var document = dataGrid.ExportToPdf(options);
document.Save("Sample.pdf");
{% endhighlight %}
{% endtabs %}
By default, group summaries in dataGrid will be exported to PDF. If you want to export without group summaries, you need to set ExportGroupSummary property as false.
{% tabs %}
{% highlight c# %}
PdfExportingOptions options = new PdfExportingOptions();
options.ExportGroupSummary = false;
var document = dataGrid.ExportToPdf(options);
document.Save("Sample.pdf");
{% endhighlight %}
{% endtabs %}
By default, table summaries in dataGrid will be exported to PDF. If you want to export without table summaries, you need to set ExportTableSummary property as false.
{% tabs %}
{% highlight c# %}
PdfExportingOptions options = new PdfExportingOptions();
options.ExportTableSummary = false;
var document = dataGrid.ExportToPdf(options);
document.Save("Sample.pdf");
{% endhighlight %}
{% endtabs %}
You can export unbound rows to PDF by setting ExportUnBoundRows property as true.
{% tabs %}
{% highlight c# %}
PdfExportingOptions options = new PdfExportingOptions();
options.ExportUnBoundRows = true;
var document = dataGrid.ExportToPdf(options);
document.Save("Sample.pdf");
{% endhighlight %}
{% endtabs %}
You can export stacked headers to PDF by setting ExportStackedHeaders property to true.
{% tabs %}
{% highlight c# %}
PdfExportingOptions options = new PdfExportingOptions();
options.ExportStackedHeaders = true;
var document = dataGrid.ExportToPdf(options);
document.Save("Sample.pdf");
{% endhighlight %}
{% endtabs %}
You can export merged cells to PDF by setting ExportMergedCells property as true.
{% tabs %} {% highlight c# %} PdfExportingOptions options = new PdfExportingOptions(); options.ExportMergedCells = true; var document = dataGrid.ExportToPdf(options); document.Save("Sample.pdf"); {% endhighlight %} {% endtabs %}
SfDataGrid provides a way to display additional content at the top (Header) or bottom (Footer) of the page while exporting to PDF. This can be achieved by setting PageHeaderFooterEventHandler in PdfExportingOptions.
You can insert string, image or any drawing in header and footer in PdfHeaderFooterEventHandler. Setting PdfPageTemplateElement to PdfHeaderFooterEventArgs.PdfDocumentTemplate.Top loads the content at top of the page and setting the PdfPageTemplateElement to PdfHeaderFooterEventArgs.PdfDocumentTemplate.Bottom loads the content at bottom of the page.
{% tabs %} {% highlight c# %} PdfExportingOptions options = new PdfExportingOptions(); options.PageHeaderFooterEventHandler = PdfHeaderFooterEventHandler; var document = dataGrid.ExportToPdf(options); document.Save("Sample.pdf");
static void PdfHeaderFooterEventHandler(object sender, PdfHeaderFooterEventArgs e)
{
PdfFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 20f, PdfFontStyle.Bold);
var width = e.PdfPage.GetClientSize().Width;
PdfPageTemplateElement header = new PdfPageTemplateElement(width, 38);
header.Graphics.DrawString("Order Details", font, PdfPens.Black, 70, 3);
e.PdfDocumentTemplate.Top = header;
}
{% endhighlight %}
{% endtabs %}
Here, string is inserted in the header of exported PDF file using DrawString method. Similarly, you can insert image, line,etc. using DrawImage, DrawLine methods respectively.
You can change the page orientation of PDF while exporting. The default page orientation is Portrait.
To change the page orientation, you need to get the exported PdfGrid by using ExportToPdfGrid method and then draw that PdfGrid into a PdfDocument by changing the PageSettings.Orientation property of PdfDocument.
{% tabs %} {% highlight c# %} var options = new PdfExportingOptions(); var document = new PdfDocument(); document.PageSettings.Orientation = PdfPageOrientation.Landscape; var page = document.Pages.Add(); var PDFGrid = dataGrid.ExportToPdfGrid(dataGrid.View, options); var format = new PdfGridLayoutFormat() { Layout = PdfLayoutType.Paginate, Break = PdfLayoutBreakType.FitPage };
PDFGrid.Draw(page, new PointF(), format); document.Save("Sample.pdf"); {% endhighlight %} {% endtabs %}
By default, entire grid will be exported to PDF. You can export selected items only by passing SelectedItems to ExportToPdf and ExportToPdfGrid methods.
{% tabs %} {% highlight c# %} PdfExportingOptions options = new PdfExportingOptions(); options.AutoColumnWidth = true; var document = dataGrid.ExportToPdf(dataGrid.SelectedItems, options); document.Save("Sample.pdf"); {% endhighlight %} {% endtabs %}
After exporting to PDF, you can save exported PDF file directly to file system by using Save method.
{% tabs %} {% highlight c# %} var document = dataGrid.ExportToPdf(); document.Save("Sample.pdf"); {% endhighlight %} {% endtabs %}
You can refer PDF documentation.
After exporting to PDF, you can save exported PDF file to stream by using Save method.
{% tabs %}
{% highlight c# %}
FileStream fileStream = new FileStream("Sample.pdf", FileMode.Create);
var document = dataGrid.ExportToPdf();
document.Save(fileStream);
fileStream.Close();
{% endhighlight %}
{% endtabs %}
You can refer PDF documentation.
After exporting to PDF, you can save exported PDF file by opening FileDialog.
{% tabs %} {% highlight c# %} var document = dataGrid.ExportToPdf(); SaveFileDialog sfd = new SaveFileDialog { Filter = "PDF Files(.pdf)|.pdf" };
if (sfd.ShowDialog() == true) { using (Stream stream = sfd.OpenFile()) { document.Save(stream); }
//Message box confirmation to view the created Pdf file.
if (MessageBox.Show("Do you want to view the Pdf file?", "Pdf file has been created",
MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes)
{
//Launching the Pdf file using the default Application.
System.Diagnostics.Process.Start(sfd.FileName);
}
}
{% endhighlight %}
{% endtabs %}
You can view exported PDF document without saving by using PDFViewerControl.
{% tabs %} {% highlight c# %} var document = this.dataGrid.ExportToPdf(); MemoryStream stream = new MemoryStream(); document.Save(stream); PdfViewerControl pdfViewer = new PdfViewerControl(); pdfViewer.Load(stream); Window window = new Window(); window.Content = pdfViewer; window.Show(); {% endhighlight %} {% endtabs %}
You can customize the cell styles based on CellType by using ExportingEventHandler.
{% tabs %} {% highlight c# %} PdfExportingOptions options = new PdfExportingOptions(); options.ExportingEventHandler = GridPdfExportingEventHandler; var document = dataGrid.ExportToPdf(options); document.Save("Sample.pdf");
void GridPdfExportingEventHandler(object sender, GridPdfExportingEventArgs e) {
if (e.CellType == ExportCellType.HeaderCell)
e.CellStyle.BackgroundBrush = PdfBrushes.LightSteelBlue;
else if (e.CellType == ExportCellType.GroupCaptionCell)
e.CellStyle.BackgroundBrush = PdfBrushes.LightGray;
else if (e.CellType == ExportCellType.RecordCell)
e.CellStyle.BackgroundBrush = PdfBrushes.Wheat;
} {% endhighlight %} {% endtabs %}
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.
{% tabs %} {% highlight c# %} PdfExportingOptions options = new PdfExportingOptions(); options.ExportingEventHandler = GridPdfExportingEventHandler; var document = dataGrid.ExportToPdf(options); document.Save("Sample.pdf");
void GridPdfExportingEventHandler(object sender, GridPdfExportingEventArgs e) {
if (e.CellType != ExportCellType.RecordCell)
return;
//creates a new font from the font file.
var font = new PdfTrueTypeFont(@"..\..\Resources\segoeui.ttf", 9f, PdfFontStyle.Regular);
e.CellStyle.Font = font;
} {% endhighlight %} {% endtabs %}
Here, new font is created from font file and it is assigned to the Font of PdfGridCell.
You can customize the cells in the PDF document by setting CellsExportingEventHandler in PdfExportingOptions.
You can customize the call values while exporting to PDF by using CellsExportingEventHandler and PdfExportingOptions.
{% tabs %} {% highlight c# %} PdfExportingOptions options = new PdfExportingOptions(); options.CellsExportingEventHandler = CellsExportingEventHandler; var document = dataGrid.ExportToPdf(options); document.Save("Sample.pdf");
private void CellsExportingEventHandler(object sender, GridCellPdfExportingEventArgs e) {
// Based on the column mapping name and the cell type, you can change the cell values while exporting to PDF.
if (e.CellType == ExportCellType.RecordCell && e.ColumnName == "IsClosed")
{
//if the cell value is True, "Y" will be displayed else "N" will be displayed.
if (e.CellValue.Equals("True"))
e.CellValue = "Y";
else
e.CellValue = "N";
}
} {% endhighlight %} {% endtabs %}
Here, cell values are changed for IsClosed column based on custom condition.
You can customize the rows based on the record values by using CellsExportingEventHandler.
{% tabs %} {% highlight c# %} PdfExportingOptions options = new PdfExportingOptions(); options.CellsExportingEventHandler = CellsExportingEventHandler; var document = dataGrid.ExportToPdf(options); document.Save("Sample.pdf");
private void CellsExportingEventHandler(object sender, GridCellPdfExportingEventArgs e) {
if (!(e.NodeEntry is OrderInfo))
return;
if ((e.NodeEntry as OrderInfo).Country == "Mexico")
{
var cellStyle = new PdfGridCellStyle();
cellStyle.BackgroundBrush = PdfBrushes.LightPink;
cellStyle.Borders.All = new PdfPen(PdfBrushes.DarkGray, 0.2f);
e.PdfGridCell.Style = cellStyle;
}
} {% endhighlight %} {% endtabs %}
By default, Middle Eastern languages (Arabic, Hebrew) in SfDataGrid are exported as left to right in PDF. You can export them as displayed in SfDataGrid (export from Right to Left) by enabling RightToLeft property in PdfStringFormat class and apply the format to the PdfGridCell by using CellsExportingEventHandler.
{% tabs %} {% highlight c# %} PdfExportingOptions options = new PdfExportingOptions(); options.CellsExportingEventHandler = CellsExportingEventHandler; var document = dataGrid.ExportToPdf(options); document.Save("Sample.pdf");
private void CellsExportingEventHandler(object sender, GridCellPdfExportingEventArgs e) {
if (e.CellType != ExportCellType.RecordCell)
return;
PdfStringFormat format = new PdfStringFormat();
//format the string from right to left.
format.RightToLeft = true;
e.PdfGridCell.StringFormat = format;
} {% endhighlight %} {% endtabs %}
By default, images which is loaded in the GridTemplateColumn will not be exported to PDF. You can export it by using CellsExportingEventHandler in PdfExportingOptions. In CellsExportingEventHandler, image is loaded in PdfGridCell.
{% tabs %} {% highlight c# %} PdfExportingOptions options = new PdfExportingOptions(); options.CellsExportingEventHandler = CellsExportingEventHandler; var document = dataGrid.ExportToPdf(options); document.Save("Sample.pdf");
private void CellsExportingEventHandler(object sender, GridCellPdfExportingEventArgs e) {
if (e.CellType == ExportCellType.RecordCell && e.ColumnName == "IsClosed")
{
var style = new PdfGridCellStyle();
PdfPen normalBorder = new PdfPen(PdfBrushes.DarkGray, 0.2f);
System.Drawing.Image image = null;
//Images are exported based on the CellValue
if (e.CellValue.Equals("True"))
{
//Access the image from the specified path
image = System.Drawing.Image.FromFile(@"..\..\Images\True.png");
}
else
image = System.Drawing.Image.FromFile(@"..\..\Images\False.png");
//Create the PDFImage for the specified image and assigned to BackgroundImage of the PdfGridCellStyle
style.BackgroundImage = PDFImage.FromImage(image);
e.PdfGridCell.ImagePosition = PdfGridImagePosition.Fit;
e.PdfGridCell.Style = style;
//customize the Border color of PdfGridCell
e.PdfGridCell.Style.Borders.All = normalBorder;
e.CellValue = null;
}
} {% endhighlight %} {% endtabs %}
By default, DetailsViewDataGrid will not be exported to PDF. You can export DetailsViewDataGrid by setting ExportDetailsView property as true.
{% tabs %} {% highlight c# %} PdfExportingOptions options = new PdfExportingOptions(); options.ExportDetailsView = true; var document = dataGrid.ExportToPdf(options); document.Save("Sample.pdf"); {% endhighlight %} {% endtabs %}
By default, only expanded DetailsViewDataGrids only will be exported to PDF document. If you want to export all the DetailsViewDataGrids, you need to set ExportAllDetails as true.
{% tabs %} {% highlight c# %} PdfExportingOptions options = new PdfExportingOptions(); options.ExportDetailsView = true; options.ExportAllDetails = true; var document = dataGrid.ExportToPdf(options); document.Save("Sample.pdf"); {% endhighlight %} {% endtabs %}
Here, first record only expanded in SfDataGrid. But all the DetailsViewDataGrid’s are shown in exported PDF document.
You can customize its exporting operation by using ChildGridExportingEventHandler.
N> While exporting DetailsViewDataGrid, FitAllColumnsInOnePage is set to true internally as horizontal pagination is not supported for DetailsViewDataGrid.
You can exclude particular DetailsViewDataGrid while exporting, by using the ChildGridExportingEventHandler and ChildGridPdfExportingEventArgs.Cancel.
{% tabs %} {% highlight c# %} PdfExportingOptions options = new PdfExportingOptions(); options.ExportDetailsView = true; options.ChildGridExportingEventHandler = ChildGridExportingEventHandler; var document = dataGrid.ExportToPdf(options); document.Save("Sample.pdf");
void ChildGridExportingEventHandler(object sender, ChildGridPdfExportingEventArgs e) { var recordEntry = e.NodeEntry as RecordEntry;
if ((recordEntry.Data as OrderInfo).OrderID == 1002)
e.Cancel = true;
} {% endhighlight %} {% endtabs %}
Here, DetailsViewDataGrid is not exported for the parent record having OrderID as 1002.
You can exclude DetailsViewDataGrid columns while exporting, by using ChildGridExportingEventHandler and ChildGridPdfExportingEventArgs.PdfExportingOptions.ExcludeColumns.
{% tabs %} {% highlight c# %} PdfExportingOptions options = new PdfExportingOptions(); options.ExportDetailsView = true; options.ChildGridExportingEventHandler = ChildGridExportingEventHandler; var document = dataGrid.ExportToPdf(options); document.Save("Sample.pdf");
void ChildGridExportingEventHandler(object sender, ChildGridPdfExportingEventArgs e) { e.PdfExportingOptions.ExcludeColumns = new List<string>() { "OrderID" }; } {% endhighlight %} {% endtabs %}
Here, OrderID column is displayed in DetailsViewDataGrid and it is excluded while exporting to PDF.
Like parent DataGrid, You can customize the DetailsViewDataGrid cells also by using CellsExportingEventHandler. Based on GridCellPdfExportingEventArgs.GridViewDefinition property, you can identify the particular DetailsViewDataGrid and customize it.
{% tabs %} {% highlight c# %} PdfExportingOptions options = new PdfExportingOptions(); options.ExportDetailsView = true; options.CellsExportingEventHandler = CellsExportingEventHandler; var document = dataGrid.ExportToPdf(options); document.Save("Sample.pdf");
private void CellsExportingEventHandler(object sender, GridCellPdfExportingEventArgs e) {
if (e.GridViewDefinition == null || e.GridViewDefinition.RelationalColumn != "ProductDetails")
return;
if (e.ColumnName == "OrderID")
{
var cellStyle = new PdfGridCellStyle();
cellStyle.BackgroundBrush = PdfBrushes.Wheat;
cellStyle.Borders.All = new PdfPen(PdfBrushes.DarkGray, 0.2f);
e.PdfGridCell.Style = cellStyle;
}
} {% endhighlight %} {% endtabs %}












