Hi,
I'm facing problem with rad grid pdf export where after exporting to pdf, data is completely misaligned. Not sure if the table format I'm using inside the item template causing the issue.
If you see the code above, I'm using three rows table format where data is present only for third row, other two rows are empty. During PDF export, third row data is getting shifted to first row and finally that is creating complete misalignment in PDF file.
I tried fixing this alignment issues in OnPDFExporting Event but it didn't work in this scenario, pasted the code below -
Please help me to investigate this issue, I'm pasting the entire piece of code below -
I'm facing problem with rad grid pdf export where after exporting to pdf, data is completely misaligned. Not sure if the table format I'm using inside the item template causing the issue.
<telerik:GridTemplateColumn UniqueName="Symbol"> <HeaderTemplate> <asp:Label ID="labelHSymbol" runat="server" Text="Symbol"></asp:Label> </HeaderTemplate> <ItemTemplate> <table width="70px"> <colgroup> <col /> </colgroup> <tr> <td> </td> </tr> <tr> <td> </td> </tr> <tr> <td> <asp:Label ID="labelSymbol" runat="server" Text='<%# Bind("Symbol") %>'></asp:Label> </td> </tr> </table> </ItemTemplate> </telerik:GridTemplateColumn>If you see the code above, I'm using three rows table format where data is present only for third row, other two rows are empty. During PDF export, third row data is getting shifted to first row and finally that is creating complete misalignment in PDF file.
I tried fixing this alignment issues in OnPDFExporting Event but it didn't work in this scenario, pasted the code below -
Approach-1:protected void TestGrid_PDFExporting(object sender, GridPdfExportingArgs e) { foreach (GridHeaderItem headerItem in gridIP.MasterTableView.GetItems(GridItemType.Header)) { headerItem.Style["background-color"] = "Gray"; } }Approach-2: protected void TestGrid_PDFExporting(object sender, GridPdfExportingArgs e) { foreach (GridHeaderItem headerItem in gridIP.MasterTableView.GetItems(GridItemType.Header)) { foreach (TableCell cell in headerItem.Cells) { cell.Style["color"] = "#FF00FF"; } } }Please help me to investigate this issue, I'm pasting the entire piece of code below -
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace PDFXLDownload{ public class MyTestEntity { public string InvName { get; set; } public string ShortName { get; set; } public string Status { get; set; } public string Symbol { get; set; } public string MSRating { get; set; } public string NERatio { get; set; } public string GERatio { get; set; } public string OneQ { get; set; } public string YT { get; set; } public string OneY { get; set; } public string ThreeY { get; set; } public string FiveY { get; set; } public string TenY { get; set; } public string ThreeYR { get; set; } public static List<MyTestEntity> GetInvestmentPerformanceData() { List<MyTestEntity> investments = new List<MyTestEntity> { new MyTestEntity { InvName = "Test Inv Name 1", ShortName="IA-RT", Status="A", Symbol = "N/A", MSRating="2", NERatio="0.46", GERatio="0.46", OneQ="00.00%", YT="00.00%", OneY="00.00%", ThreeY="00.00%", FiveY="00.00%", TenY="00.00%", ThreeYR="00.00%" }, new MyTestEntity { InvName = "Test Inv Name 2", ShortName="IA-YE", Status="C", Symbol = "N/A", MSRating="2", NERatio="0.46", GERatio="0.46", OneQ="00.00%", YT="00.00%", OneY="00.00%", ThreeY="00.00%", FiveY="00.00%", TenY="00.00%", ThreeYR="00.00%" }, new MyTestEntity { InvName = "Test Inv Name 3", ShortName="IA-RT", Status="B", Symbol = "N/A", MSRating="3", NERatio="0.46", GERatio="0.46", OneQ="00.00%", YT="00.00%", OneY="00.00%", ThreeY="00.00%", FiveY="00.00%", TenY="00.00%", ThreeYR="00.00%" }, new MyTestEntity { InvName = "Test Inv Name 4", ShortName="IA-RT", Status="D", Symbol = "N/A", MSRating="4", NERatio="0.46", GERatio="0.46", OneQ="00.00%", YT="00.00%", OneY="00.00%", ThreeY="00.00%", FiveY="00.00%", TenY="00.00%", ThreeYR="00.00%" } }; return investments; } }}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PDFXLDownload.aspx.cs"
Inherits="PDFXLDownload.PDFXLDownload" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!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>
<link href="Styles/Site.css" rel="stylesheet" type="text/css" />
<link href="Grid.Default.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<telerik:RadScriptManager ID="RadScriptManager1" runat="server" EnableScriptCombine="false">
</telerik:RadScriptManager>
<telerik:RadGrid runat="server" ID="gridIP" EnableEmbeddedSkins="false" GridLines="None"
ShowFooter="false" AllowFilteringByColumn="false" AllowSorting="false" ShowGroupPanel="false"
AllowPaging="false" OnPdfExporting="TestGrid_PDFExporting">
<MasterTableView AutoGenerateColumns="False" CommandItemDisplay="Bottom">
<Columns>
<telerik:GridTemplateColumn UniqueName="InvName">
<HeaderTemplate>
<asp:Label ID="labelHInvName" runat="server" Text="Inv Name"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<table width="200px">
<colgroup>
<col />
</colgroup>
<tr>
<td>
<asp:Label ID="label1" runat="server" Text="AC Test Data One"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="label2" runat="server" Text="AC Test Data Two"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="labelInvName" runat="server" Text='<%# Bind("InvName") %>'></asp:Label>
<asp:Label ID="labelShortName" runat="server" Text='<%# Bind("ShortName") %>'></asp:Label>
<asp:Label ID="labelStatus" runat="server" CssClass="FundStatus" Text='<%# Bind("Status") %>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn UniqueName="Symbol">
<HeaderTemplate>
<asp:Label ID="labelHSymbol" runat="server" Text="Symbol"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<table width="70px">
<colgroup>
<col />
</colgroup>
<tr>
<td>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
<asp:Label ID="labelSymbol" runat="server" Text='<%# Bind("Symbol") %>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn UniqueName="MSRating">
<HeaderTemplate>
<asp:Label ID="labelHMSRating" runat="server" Text="MSRating"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<table width="70px">
<colgroup>
<col />
</colgroup>
<tr>
<td>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
<asp:Label ID="labelMSRating" runat="server" Text='<%# Bind("MSRating") %>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn UniqueName="NERatio">
<HeaderTemplate>
<asp:Label ID="labelHNERatio" runat="server" Text="NERatio"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<table width="70px">
<colgroup>
<col />
</colgroup>
<tr>
<td>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
<asp:Label ID="labelNERatio" runat="server" Text='<%# Bind("NERatio") %>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn UniqueName="GERatio">
<HeaderTemplate>
<asp:Label ID="labelHGERatio" runat="server" Text="GERatio"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<table width="70px">
<colgroup>
<col />
</colgroup>
<tr>
<td>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
<asp:Label ID="labelGERatio" runat="server" Text='<%# Bind("GERatio") %>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn UniqueName="OneQ">
<HeaderTemplate>
<asp:Label ID="labelHOneQ" runat="server" Text="OneQ"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<table width="70px">
<colgroup>
<col />
</colgroup>
<tr>
<td>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
<asp:Label ID="labelOneQ" runat="server" Text='<%# Bind("OneQ") %>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn UniqueName="YT">
<HeaderTemplate>
<asp:Label ID="labelHYT" runat="server" Text="YTenure"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<table width="70px">
<colgroup>
<col />
</colgroup>
<tr>
<td>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
<asp:Label ID="labelYT" runat="server" Text='<%# Bind("YT") %>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn UniqueName="OneY">
<HeaderTemplate>
<asp:Label ID="labelHOneY" runat="server" Text="OneY"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<table width="70px">
<colgroup>
<col />
</colgroup>
<tr>
<td>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
<asp:Label ID="labelOneY" runat="server" Text='<%# Bind("OneY") %>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn UniqueName="ThreeY">
<HeaderTemplate>
<asp:Label ID="labelHThreeY" runat="server" Text="ThreeY"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<table width="70px">
<colgroup>
<col />
</colgroup>
<tr>
<td>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
<asp:Label ID="labelThreeY" runat="server" Text='<%# Bind("ThreeY") %>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn UniqueName="FiveY">
<HeaderTemplate>
<asp:Label ID="labelHFiveY" runat="server" Text="FiveY"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<table width="70px">
<colgroup>
<col />
</colgroup>
<tr>
<td>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
<asp:Label ID="labelFiveY" runat="server" Text='<%# Bind("FiveY") %>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn UniqueName="TenY">
<HeaderTemplate>
<asp:Label ID="labelHTenY" runat="server" Text="TenY"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<table width="70px">
<colgroup>
<col />
</colgroup>
<tr>
<td>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
<asp:Label ID="labelTenY" runat="server" Text='<%# Bind("TenY") %>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn UniqueName="ThreeYR">
<HeaderTemplate>
<asp:Label ID="labelHThreeYR" runat="server" Text="ThreeYR"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<table width="70px">
<colgroup>
<col />
</colgroup>
<tr>
<td>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
<asp:Label ID="labelThreeYR" runat="server" Text='<%# Bind("ThreeYR") %>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
<CommandItemStyle HorizontalAlign="Left" Wrap="true" Width="100%" />
<CommandItemTemplate>
<asp:Label ID="lblText" runat="server" Visible="false" Width="100%"></asp:Label>
</CommandItemTemplate>
</MasterTableView>
</telerik:RadGrid>
<asp:Button ID="DownloadPDFButton" runat="server" OnClick="DownloadPDFButton_Click"
Text="Download PDF" Width="184px" />
<asp:Button ID="DownloadXLButton" runat="server" OnClick="DownloadXLButton_Click"
Text="Download Exel" Width="157px" />
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.IO;
namespace PDFXLDownload
{
public partial class PDFXLDownload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
gridIP.DataSource = MyTestEntity.GetInvestmentPerformanceData();
gridIP.DataBind();
}
protected void TestGrid_PDFExporting(object sender, GridPdfExportingArgs e)
{
string rowHtml = e.RawHTML.ToString();
e.RawHTML = rowHtml.Replace("<td></td>", "<td style=\"height:20px\"></td>");
}
protected void DownloadPDFButton_Click(object sender, EventArgs e)
{
gridIP.ExportSettings.OpenInNewWindow = true;
gridIP.ExportSettings.FileName = "PDFExportDemo";
gridIP.ExportSettings.ExportOnlyData = false;
//Landscape
gridIP.ExportSettings.Pdf.PageHeight = Unit.Parse("10in");
gridIP.ExportSettings.Pdf.PageWidth = Unit.Parse("16in");
//// 1/2 inch margins
gridIP.ExportSettings.Pdf.PageLeftMargin = Unit.Parse("1in");
gridIP.ExportSettings.Pdf.PageRightMargin = Unit.Parse("1in");
gridIP.ExportSettings.Pdf.PageTopMargin = Unit.Parse("1in");
gridIP.ExportSettings.Pdf.PageBottomMargin = Unit.Parse("1in");
gridIP.ExportSettings.FileName = "Test Pdf File";
gridIP.MasterTableView.Columns.FindByUniqueName("InvName").HeaderStyle.Width = Unit.Pixel(200);
gridIP.MasterTableView.ExportToPdf();
}
protected void DownloadXLButton_Click(object sender, EventArgs e)
{
gridIP.ExportSettings.OpenInNewWindow = true;
gridIP.ExportSettings.FileName = "MyTestEntity";
gridIP.ExportSettings.Excel.FileExtension = "xls";
gridIP.MasterTableView.ExportToExcel();
}
}
}