I want to export RadGrid styles to PDF. I have a radgrid in a normal .aspx page, and I use a usercontrol to export the radgrid to pdf format. I have read the tips and tricks article, but am still unable to get exporting styles to PDF to work.
Here is the radgrid in a normal aspx page:
| <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> |
| <%@ Register TagPrefix="uc1" TagName="Export" Src="~/Controls/ctrlExportReport.ascx" %> |
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| <html xmlns="http://www.w3.org/1999/xhtml"> |
| <head runat="server"> |
| <title></title> |
| </head> |
| <body> |
| <form id="form1" runat="server"> |
| <div> |
| <asp:ScriptManager ID="sm1" runat="server"></asp:ScriptManager> |
| <table style="width: 100%"> |
| <tr> |
| <td> |
| <telerik:radwindowmanager id="rwm" runat="server" modal="true" reloadonshow="true" visibletitlebar="True" behavior="close, move" visiblestatusbar="false"> |
| </telerik:radwindowmanager> |
| <telerik:RadAjaxLoadingPanel id="rdpInterventions" runat="server" cssclass="DataGrid" style="text-align: center; background-color: White; width: 100%"> |
| <p class="header"> |
| Loading interventions due within the selected date range</p> |
| <br /> |
| <p> |
| <img id="Img1" alt="loading..." runat="server" src="~/Images/LoadingProgressBar.gif" /></p> |
| </telerik:RadAjaxLoadingPanel> |
| <telerik:radajaxpanel id="rap" runat="server" enableajax="true" LoadingPanelID="rdpInterventions"> |
| <Telerik:radgrid id="rg" runat="server" autogeneratecolumns="true" width="100%" Skin="" > |
| <mastertableview allowsorting="true" cssclass="DataGrid" gridlines="horizontal" showfooter="true" width="100%"> |
| <HeaderStyle forecolor="blue" BackColor="Black" /> |
| <DetailTables> |
| <telerik:GridTableView DataMember="Detail" DataKeyNames="SourceID" Width="95%"> |
| <ParentTableRelation> |
| <telerik:GridRelationFields MasterKeyField="SourceID" DetailKeyField="SourceID" /> |
| </ParentTableRelation> |
| </telerik:GridTableView> |
| </DetailTables> |
| </MasterTableView> |
| </telerik:RadGrid> |
| </telerik:radajaxpanel> |
| </td> |
| </tr> |
| <tr> |
| <td style="height: 25px"> |
| <uc1:Export ID="ctrlExport1" runat="server" ControlName="rg" /> |
| </td> |
| </tr> |
| </table> |
| </div> |
| </form> |
| </body> |
| </html> |
Here is the code for the usercontrol:
| <%@ Control Language="VB" AutoEventWireup="false" CodeFile="ctrlExportReport.ascx.vb" Inherits="ctrlExportReport" %> |
| <asp:ImageButton ID="btnExportToExcel" runat="server" AlternateText="Export to Excel" ImageUrl="~/Images/Export2Excel.gif" ToolTip="Export to Excel" /> |
| |
| <asp:ImageButton ID="btnExportToPDF" runat="server" AlternateText="Export to PDF" ImageUrl="~/Images/Export2Pdf.gif" ToolTip="Export to PDF" /> |
| <input id="hdnHidePDF" runat="Server" type="hidden" /> |
And here is the code-behind for the user control:
| ''' <summary> |
| ''' Export a RadGrid to either Excel or PDF. |
| ''' </summary> |
| Partial Class ctrlExportReport |
| Inherits System.Web.UI.UserControl |
| #Region " Declarations " |
| 'The ID of the RadGrid displaying the report. |
| Private strControlName As String = String.Empty |
| #End Region 'Declarations |
| #Region " Properties " |
| 'The ID of the RadGrid displaying the report. |
| Public Property ControlName() As String |
| Get |
| If String.IsNullOrEmpty(strControlName) = False Then |
| Return strControlName |
| End If |
| Return String.Empty |
| End Get |
| Set(ByVal value As String) |
| strControlName = value |
| End Set |
| End Property |
| 'Boolean whether or not to display the PDF button. |
| Public Property HidePDF() As Boolean |
| Get |
| If String.IsNullOrEmpty(hdnHidePDF.Value) Then |
| Return False |
| Else |
| Return hdnHidePDF.Value |
| End If |
| End Get |
| Set(ByVal value As Boolean) |
| hdnHidePDF.Value = value |
| End Set |
| End Property |
| 'Boolean whether or not to display the page title. |
| Public Property IncludePageTitle() As Boolean |
| Get |
| If ViewState.Item("IncludePageTitle") Is Nothing Then |
| Return False |
| Else |
| Return ViewState.Item("IncludePageTitle") |
| End If |
| End Get |
| Set(ByVal value As Boolean) |
| ViewState.Item("IncludePageTitle") = value |
| End Set |
| End Property |
| 'The BaseFileName constant with the current date appended. |
| Private ReadOnly Property FileName() As String |
| Get |
| Return String.Format("{0}-{1}-{2}", Now.Year.ToString(), PadDatePart(Now.Month), PadDatePart(Now.Day)).Insert(0, AppFileName) |
| End Get |
| End Property |
| Public Property PageTitle() As String |
| Get |
| If ViewState.Item("PageTitle") Is Nothing Then |
| Return ConfigurationManager.AppSettings("DefaultPageTitle") |
| Else |
| Return ViewState.Item("PageTitle").ToString() |
| End If |
| End Get |
| Set(ByVal value As String) |
| ViewState.Item("PageTitle") = value |
| End Set |
| End Property |
| Private ReadOnly Property AppFileName() As String |
| Get |
| If Not String.IsNullOrEmpty(ConfigurationManager.AppSettings("AppFileName")) Then |
| Return ConfigurationManager.AppSettings("AppFileName").ToString |
| Else |
| Return "GIC" |
| End If |
| End Get |
| End Property |
| Private ReadOnly Property Report() As RadGrid |
| Get |
| If String.IsNullOrEmpty(Me.ControlName) = False Then |
| Dim objControl As Control = Parent.FindControl(Me.ControlName) |
| If objControl IsNot Nothing Then |
| Try |
| Return DirectCast(objControl, RadGrid) |
| Catch ex As Exception |
| 'objControl could not be cast to a RadGrid. Free |
| 'resources and return Nothing. |
| objControl = Nothing |
| End Try |
| End If |
| End If |
| Return Nothing |
| End Get |
| End Property |
| #End Region 'Properties |
| #Region " Events " |
| Protected Sub btnExportToExcel_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnExportToExcel.Click |
| If Me.Report IsNot Nothing Then |
| Me.ConfigureExportSettings() |
| Report.MasterTableView.ExportToExcel() |
| End If |
| End Sub |
| Protected Sub btnExportToPDF_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnExportToPDF.Click |
| If Me.Report IsNot Nothing Then |
| '-- 1) Expand and rebind the grid -- |
| Me.Report.MasterTableView.HierarchyDefaultExpanded = True |
| Me.Report.Rebind() |
| '-- 2) Setup the export settings -- |
| Me.ConfigureExportSettings(True) |
| '-- 3) Export the report -- |
| With Report |
| .Page.Response.ClearHeaders() |
| .Page.Response.Cache.SetCacheability(HttpCacheability.Private) |
| .MasterTableView.ExportToPdf() |
| End With |
| End If |
| End Sub |
| ' Check to see if the HidePDF property has been set - if so, hide that button. |
| Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender |
| If HidePDF Then |
| Me.btnExportToPDF.Visible = False |
| End If |
| End Sub |
| #End Region 'Events |
| #Region " Methods " |
| 'These settings are shared by both Excel and PDF. |
| Private Sub ConfigureExportSettings(Optional ByVal bPdf As Boolean = False) |
| '-- 1) Set the global report export settings -- |
| With Report.ExportSettings |
| .OpenInNewWindow = True 'Makes GUI cleaner. |
| .FileName = Me.FileName |
| .ExportOnlyData = True 'Header will be included. |
| .IgnorePaging = True 'All Records - setting this to true means if the grid is collapsed by default, the PDF will not show |
| ' the detail items even if the user expands the parent items manually. Conversely, if the grid is expanded by default, the |
| ' pdf will show the detail items even if the user collapses them. |
| End With |
| '-- 2) Set the msater table font and styles -- |
| With Report.MasterTableView.Font |
| .Name = "Arial" |
| .Size = New FontUnit(9, UnitType.Point) |
| End With |
| With Report.MasterTableView.HeaderStyle.Font |
| .Name = "Arial" |
| .Size = New FontUnit(9, UnitType.Point) |
| End With |
| '-- 3) Hide any of the command items (if applicable) -- |
| For Each commandItem As GridItem In Report.MasterTableView.GetItems(GridItemType.CommandItem) |
| commandItem.Visible = False |
| Next |
| '-- 4) Set the Pdf Export Settings if doing the Pdf export -- |
| If bPdf Then |
| With Report.ExportSettings.Pdf |
| If IncludePageTitle Then |
| .PageTitle = PageTitle |
| Else |
| .PageTitle = "" |
| End If |
| .PageTopMargin = New Unit(0.7, UnitType.Inch) |
| .FontType = Telerik.Web.Apoc.Render.Pdf.FontType.Embed |
| .PageBottomMargin = New Unit(0.4, UnitType.Inch) |
| .PageLeftMargin = .PageBottomMargin |
| .PageRightMargin = .PageBottomMargin |
| End With |
| Dim headerItem As GridHeaderItem |
| For Each headerItem In Report.MasterTableView.GetItems(GridItemType.Header) |
| headerItem.Style("font-size") = "16pt" |
| headerItem.Style("color") = "red" |
| headerItem.Style("background-color") = "yellow" |
| headerItem.Style("height") = "50px" |
| headerItem.Style("vertical-align") = "bottom" |
| For Each cell As TableCell In headerItem.Cells |
| cell.Style("text-align") = "left" |
| cell.Style("font-weight") = "bold" |
| cell.Style("border-color") = "red" |
| Next |
| Next |
| End If |
| End Sub |
| Private Function PadDatePart( _ |
| ByVal intDatePart As Integer) As String |
| Dim strDatePart As String = intDatePart.ToString() |
| If strDatePart.Length = 1 Then |
| strDatePart = "0" & strDatePart |
| End If |
| Return strDatePart |
| End Function |
| #End Region 'Methods |
| End Class 'Partial "ctrlExportReport" |
Can you help me figure out what I am doing wrong?
Thanks!
Jill