How to customize PDF Exporting options in GridDataControl?
GridDataControl allows you to export the Grid to pdf using the following extension methods ExportToPdf, ExportToPdfDocument, ExportToPdfGrid and ExportToPdfGridDocument that are in the namespace Syncfusion.Windows.Controls.Grid.Converter.
You can customize the exporting by using “ExportToPdfDocument” and “ExportToPdf” methods that has ExportToPdfOptions as parameter. ExportToPdfOptions provides various options to customize the exporting as per your requirement.
The list of properties available in ExportToPdfOptions class is as follows
S. No | API Name | Description |
1 | ExportStyles | When set to ‘True’, ExportToPdf method exports the Grid to pdf with styles, otherwise exports the Grid to pdf without any styles (background, foreground, font etc.) Default Value : True |
2 | RepeatHeaders | A Boolean variable determines whether the Header rows are placed in all pdf pages or not. Default Value : True |
3 | IncludeHeaders | A Boolean variable determines whether the Header rows exports or not. Default Value : True |
4 | AutoColumnWidth | When set to ‘True’, width is automatically set based on the cell values, otherwise width is copied from Grid. Default Value : False |
5 | ExportNestedGrid | A Boolean variable determines whether the ExportToPdf method exports the NestedGrid or not. Default Value : False |
6 | ExcludeColumns | A List<string>. You can exclude the columns by adding the mapping name to “ExcludeColumns” API. |
7 | ExportAddNewRow | A Boolean variable determines whether the AddNewRow is exported or not. DefaultValue: False |
8 | ExportFilterBar | A Boolean variable determines whether the FilterBar is exported or not. Default Value : False |
You can refer the following code sample to customize the Pdf Exporting Options.
C#
using Syncfusion.Windows.Controls.Grid.Converter; using Syncfusion.Pdf.Grid; using Syncfusion.Pdf.Graphics; var sfd = new SaveFileDialog //used to get filename for pdf { DefaultExt = ".pdf", Filter = "Adobe PDF Files(*.pdf)|*.pdf", FilterIndex = 1 }; if (sfd.ShowDialog() == true) { using (Stream stream = sfd.OpenFile()) { ExportToPdfOptions exportToPdfOptions = new ExportToPdfOptions(); exportToPdfOptions.ExportStyles = false; exportToPdfOptions.AutoColumnWidth = true; exportToPdfOptions.ExcludeColumns.Add("Freight"); exportToPdfOptions.RepeatHeaders = false; PdfGrid pdfGrid = Grid.Model.ExportToPdf(GridRangeInfo.Table(), exportToPdfOptions); //Creating PDFDocument to drag PDFGrid var pdfDocument = new PdfDocument(); var page = pdfDocument.Pages.Add(); //Defining Layout format var format = new PdfGridLayoutFormat { Layout = PdfLayoutType.Paginate, Break = PdfLayoutBreakType.FitElement }; //Drawing PDFGrid to PDFDocument pdfGrid.Draw(page, new PointF(), format); pdfDocument.Save(stream); Process.Start(sfd.FileName); } }
After getting the file name, you can create a new PdfDocument by exporting the Grid. Here the property ExportStyles is set as ‘False’ so that when you export to pdf, styles are not exported in order to improve the export performance. You can exclude Freight column from export by adding it to ExcludeColumns property. By setting the property RepeatHeaders as ‘False’, you can avoid the repetition of headers at every page in the exported pdf.
You can change the font and style of PDFGrid before drawing to PDFdocument. You can refer the following link for more information to customize PDFGrid.
http://help.syncfusion.com/wpf/pdf
The following screenshots illustrate the exported Pdf files using ExportToPdfDocument.
When ExportStyles is set as True:
When ExportStyles is set as False: