RadGridView can export its contents to PDF using two separate mechanisms.
Before running export to PDF, you have to initialize the GridViewPdfExport class. The constructor takes one parameter: RadGridView which will be exported:
Telerik.WinControls.Export.GridViewPdfExport pdfExporter = new Telerik.WinControls.Export.GridViewPdfExport(this.radGridView1);
Dim pdfExporter As New Telerik.WinControls.Export.GridViewPdfExport(Me.RadGridView1)
The FileExtension property allows you to change the default (*.pdf) file extension of the exported file:
pdfExporter.FileExtension = ".pdf";
pdfExporter.FileExtension = ".pdf"
GridViewPdfExport uses the default enumeration of hidden column and row settings. You can choose one of the three options by setting HiddenColumnOption and HiddenRowOption properties. However, PDF do not support real hidden columns, so choosing the ExportAsHidden will not behave the same as ExportAlways.
pdfExporter.HiddenColumnOption = Telerik.WinControls.UI.Export.HiddenOption.DoNotExport;
pdfExporter.HiddenColumnOption = Telerik.WinControls.UI.Export.HiddenOption.DoNotExport
Before applying customizations to the headers and footers we need to enable them:
pdfExporter.ShowHeaderAndFooter = true;
pdfExporter.ShowHeaderAndFooter = True
pdfExporter.HeaderHeight = 30; pdfExporter.HeaderFont = new Font("Arial", 22); pdfExporter.Logo = System.Drawing.Image.FromFile(@"C:\MyLogo.png"); pdfExporter.LeftHeader = "[Logo]"; pdfExporter.LogoAlignment = ContentAlignment.MiddleLeft; pdfExporter.LogoLayout = Telerik.WinControls.Export.LogoLayout.Fit; pdfExporter.MiddleHeader = "Middle header"; pdfExporter.RightHeader = "Right header"; pdfExporter.ReverseHeaderOnEvenPages = true; pdfExporter.FooterHeight = 30; pdfExporter.FooterFont = new Font("Arial", 22); pdfExporter.LeftFooter = "Left footer"; pdfExporter.MiddleFooter = "Middle footer"; pdfExporter.RightFooter = "Right footer"; pdfExporter.ReverseFooterOnEvenPages = true;
pdfExporter.HeaderHeight = 30 pdfExporter.HeaderFont = New Font("Arial", 22) pdfExporter.Logo = System.Drawing.Image.FromFile("C:\MyLogo.png") pdfExporter.LeftHeader = "[Logo]" pdfExporter.LogoAlignment = ContentAlignment.MiddleLeft pdfExporter.LogoLayout = Telerik.WinControls.Export.LogoLayout.Fit pdfExporter.MiddleHeader = "Middle header" pdfExporter.RightHeader = "Right header" pdfExporter.ReverseHeaderOnEvenPages = True pdfExporter.FooterHeight = 30 pdfExporter.FooterFont = New Font("Arial", 22) pdfExporter.LeftFooter = "Left footer" pdfExporter.MiddleFooter = "Middle footer" pdfExporter.RightFooter = "Right footer" pdfExporter.ReverseFooterOnEvenPages = True
The HeaderExported event can be used to perform custom drawing in the header. The following example shows how you can draw a two line header.
The SummariesExportOption property to specifies how to export summary items. There are four options to choose:
pdfExporter.SummariesExportOption = SummariesOption.ExportAll;
pdfExporter.SummariesExportOption = SummariesOption.ExportAll
Use this property to make the grid fits to the PDF page width.
pdfExporter.FitToPageWidth = true;
pdfExporter.FitToPageWidth = True
You can use Scale to change the grid size on the PDF. For example if Scale = 1.2f means the grid will be 20% bigger.
pdfExporter.Scale = 1.2;
pdfExporter.Scale = 1.2
PDF Export Settings
The PDFExportSettings property supports various settings on PDF file level. You can set the following:
pdfExporter.ExportSettings.Description = "Document Description";
pdfExporter.ExportSettings.Description = "Document Description"
ExportViewDefinition
Gets or sets a value indicating whether to export the view definition.
ChildViewExportMode: Defines which child view of a hierarchy row to be exported. Available modes are:
The PDFExportSettings property supports various settings on PDF file level. You can set the following:
pdfExporter.ExportSettings.Description = "Document Description";
pdfExporter.ExportSettings.Description = "Document Description"
Two methods are responsible for exporting data to PDF. Both receive as a parameter the file name.
string fileName = "c:\\ExportedData.pdf"; pdfExporter.RunExport(fileName, new Telerik.WinControls.Export.PdfExportRenderer());
Dim fileName As String = "c:\ExportedData.pdf" pdfExporter.RunExport(fileName, New Telerik.WinControls.Export.PdfExportRenderer())
The RunExport method has several overloads allowing the user to export using a stream as well:
string exportFile = @"..\..\exportedData.pdf"; using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) < Telerik.WinControls.Export.GridViewPdfExport exporter = new Telerik.WinControls.Export.GridViewPdfExport(this.radGridView1); Telerik.WinControls.Export.PdfExportRenderer renderer = new Telerik.WinControls.Export.PdfExportRenderer(); exporter.RunExport(ms, renderer); using (System.IO.FileStream fileStream = new System.IO.FileStream(exportFile, FileMode.Create, FileAccess.Write)) < ms.WriteTo(fileStream); >>
Dim exportFile As String = "..\..\exportedData.pdf" Using ms As New System.IO.MemoryStream() Dim exporter As New Telerik.WinControls.Export.GridViewPdfExport(Me.RadGridView1) Dim renderer As New Telerik.WinControls.Export.PdfExportRenderer() exporter.RunExport(ms, renderer) Using fileStream As New System.IO.FileStream(exportFile, FileMode.Create, FileAccess.Write) ms.WriteTo(fileStream) End Using End Using
string fileNameAsync = "c:\\ExportedDataAsync.pdf"; pdfExporter.RunExportAsync(fileNameAsync, new Telerik.WinControls.Export.PdfExportRenderer());
Dim fileNameAsync As String = "c:\ExportedDataAsync.pdf" pdfExporter.RunExportAsync(fileNameAsync, New Telerik.WinControls.Export.PdfExportRenderer())
The RunExportAsync method has several overloads allowing the user to export using a stream as well:
private void radButton1_Click(object sender, EventArgs e) < System.IO.MemoryStream ms = new System.IO.MemoryStream(); Telerik.WinControls.Export.GridViewPdfExport pdfExporter = new Telerik.WinControls.Export.GridViewPdfExport(this.radGridView1); Telerik.WinControls.Export.PdfExportRenderer renderer = new Telerik.WinControls.Export.PdfExportRenderer(); pdfExporter.AsyncExportCompleted += pdfExporter_AsyncExportCompleted; pdfExporter.RunExportAsync(ms, renderer); >private void pdfExporter_AsyncExportCompleted(object sender, AsyncCompletedEventArgs e) < RunWorkerCompletedEventArgs args = e as RunWorkerCompletedEventArgs; string exportFile = @"..\..\exportedAsyncData.pdf"; using (System.IO.FileStream fileStream = new System.IO.FileStream(exportFile, FileMode.Create, FileAccess.Write)) < MemoryStream ms = args.Result as MemoryStream; ms.WriteTo(fileStream); ms.Close(); >>
Private Sub radButton1_Click(sender As Object, e As EventArgs) Dim ms As New System.IO.MemoryStream() Dim pdfExporter As New Telerik.WinControls.Export.GridViewPdfExport(Me.RadGridView1) Dim renderer As New Telerik.WinControls.Export.PdfExportRenderer() AddHandler pdfExporter.AsyncExportCompleted, AddressOf pdfExporter_AsyncExportCompleted pdfExporter.RunExportAsync(ms, renderer) End Sub Private Sub pdfExporter_AsyncExportCompleted(sender As Object, e As AsyncCompletedEventArgs) Dim args As RunWorkerCompletedEventArgs = TryCast(e, RunWorkerCompletedEventArgs) Dim exportFile As String = "..\..\exportedAsyncData.pdf" Using fileStream As New System.IO.FileStream(exportFile, FileMode.Create, FileAccess.Write) Dim ms As MemoryStream = TryCast(args.Result, MemoryStream) ms.WriteTo(fileStream) ms.Close() End Using End Sub
Initialization
Before running export to PDF, you have to initialize the ExportToPDF class. The constructor takes one parameter: RadGridView which will be exported:
ExportToPDF exporter = new ExportToPDF(this.radGridView1);
Dim exporter As New ExportToPDF(Me.RadGridView1)
The FileExtension property allows you to change the default (*.pdf) file extension of the exported file:
exporter.FileExtension = "pdf";
exporter.FileExtension = "pdf"
ExportToPDF uses the default enumeration of hidden column and row settings. You can choose one of the three options by setting HiddenColumnOption and HiddenRowOption properties. However, PDF do not support real hidden columns, so choosing the ExportAsHidden will not behave the same as ExportAlways.
exporter.HiddenColumnOption = Telerik.WinControls.UI.Export.HiddenOption.DoNotExport;
exporter.HiddenColumnOption = Telerik.WinControls.UI.Export.HiddenOption.DoNotExport
Using the ExportToPDF class allows you to export the visual settings (themes) to the PDF file. ExportToPDF also provides a visual representation of the alternating row color. This feature works only if EnableAlternatingRow property is set to true. Note that it does not transfer the alternating row settings that come from the theme of the control. RadGridView will also export the conditional formatting to the PDF file. You can enable exporting visual settings through the ExportVisualSettings property. The default value of this property is false.
exporter.ExportVisualSettings = true;
exporter.ExportVisualSettings = True
You can add a page title which will be presented on every page of the PDF document through __PageTitle__property.
exporter.PageTitle = "Title";
exporter.PageTitle = "Title"
You can use SummariesExportOption property to specify how to export summary items. There are four options to choose:
exporter.SummariesExportOption = SummariesOption.ExportAll;
exporter.SummariesExportOption = SummariesOption.ExportAll
Use this property to make the grid fits to the PDF page width.
exporter.FitToPageWidth = true;
exporter.FitToPageWidth = True
You can use Scale to change the grid size on the PDF. For example if Scale = 1.2f means the grid will be 20% bigger.
exporter.Scale = 1.2f;
exporter.Scale = 1.2F
This property controls the thickness of the table border. The default value is 0 and border is not drawn.
exporter.TableBorderThickness = 1;
exporter.TableBorderThickness = 1
The PDFExportSettings property supports various settings on PDF file level. You can set the following:
exporter.PdfExportSettings.PageHeight = 210; exporter.PdfExportSettings.PageWidth = 297;
exporter.PdfExportSettings.PageHeight = 210 exporter.PdfExportSettings.PageWidth = 297
Exporting data to PDF is done through the RunExport method of ExportToPDF object. The RunExport method accepts the following parameter:
Consider the code sample below:
string fileName = "c:\\ExportedData.pdf"; exporter.RunExport(fileName);
Dim fileName As String = "c:\ExportedData.pdf" exporter.RunExport(fileName)
HTMLCellFormating event: Since the the export process first renders RadGridView in XHTML format you can use the event which comes from ExportToHTML class: HTMLCellFormatting. It gives access to a single cell HTML element that allows you to make additional formatting for every HTML cell related to the exported RadGridView:
void exporter_HTMLCellFormatting(object sender, Telerik.WinControls.UI.Export.HTML.HTMLCellFormattingEventArgs e) < if (e.GridColumnIndex == 1 && e.GridRowInfoType == typeof(GridViewDataRowInfo)) < e.HTMLCellElement.Value = "Test value"; e.HTMLCellElement.Styles.Add("background-color", ColorTranslator.ToHtml(Color.Orange)); >>
Private Sub exporter_HTMLCellFormatting(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.Export.HTML.HTMLCellFormattingEventArgs) If e.GridColumnIndex = 1 AndAlso e.GridRowInfoType.Equals(GetType(GridViewDataRowInfo)) Then e.HTMLCellElement.Value = "Test value" e.HTMLCellElement.Styles.Add("background-color", ColorTranslator.ToHtml(Color.Orange)) End If End Sub
ExportToPDF supports all left-to-right languages when the appropriate Unicode font is set. The most common international font is Arial Unicode MS, because it covers all Unicode characters. Of course, you can use other-specific fonts such as Batang for Korean, SimSun for Chinese, MS Mincho for Japanese, etc.
void pdfExporter_HTMLCellFormatting(object sender, HTMLCellFormattingEventArgs e) < //The following sets unicode font for every cell. e.HTMLCellElement.Styles.Remove("font-family"); e.HTMLCellElement.Styles.Add("font-family", "Arial Unicode MS"); >
Private Sub pdfExporter_HTMLCellFormatting(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.Export.HTML.HTMLCellFormattingEventArgs) 'The following sets unicode font for every cell. e.HTMLCellElement.Styles.Remove("font-family") e.HTMLCellElement.Styles.Add("font-family", "Arial Unicode MS") End Sub