This is a migrated thread and some comments may be shown as answers.

PDF Export - Data is not showing center aligned

7 Answers 598 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Vivek
Top achievements
Rank 2
Vivek asked on 24 May 2013, 12:42 PM
Hi,

We are exporting the rad grid data in pdf format where data is not getting center aligned, attaching the code, please have a look and let us know what's the solution for this problem.

This is really urgent to resolve, please reply asap.

PFA  -  Snap shot of presently exported pdf file where data is not center aligned.

<%@ 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">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server" EnableScriptCombine="false">
    </telerik:RadScriptManager>
     <telerik:RadGrid runat="server" ID="RadGrid2" EnableEmbeddedSkins="false" GridLines="None"
        ShowFooter="false" AllowPaging="false" OnPdfExporting="TestGrid2_PDFExporting" OnItemCreated="RadGrid2_ItemCreated" >
        <ExportSettings OpenInNewWindow="true">
            <Pdf PageHeight="210mm" PageWidth="297mm" PageBottomMargin="20mm" PageTopMargin="20mm"
                PageLeftMargin="20mm" PageRightMargin="20mm"></Pdf>
        </ExportSettings>
        <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>
                            <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>
                            <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>
                            <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>
                            <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>
                            <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>
                            <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>
                            <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>
                            <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>
                            <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>
                            <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 >
                            <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>
                            <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;
using System.Text.RegularExpressions;
using System.Globalization;
  
namespace PDFXLDownload
{
    public partial class PDFXLDownload : System.Web.UI.Page
    {
        bool isPdfExport = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            RadGrid2.DataSource = MyDataSource;
            RadGrid2.DataBind();
        }
  
    
        protected void RadGrid2_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (isPdfExport)
            {
                FormatGridCells(e.Item);
            }
        }
          
        private void FormatGridCells(GridItem item)
        {
            if (item is GridHeaderItem)
            {             
                
                foreach (TableCell cell in item.Cells)
                {
                    cell.Style["text-align"] = "center";
                }
            }
            if (item is GridItem)
            {              
                foreach (TableCell cell in item.Cells)
                {
                    cell.Style["text-align"] = "center";
                }               
            }
            item.PrepareItemStyle();
        }
  
              protected void DownloadPDFButton_Click(object sender, EventArgs e)
        {
            // RadGrid2.GridLines = GridLines.Both; //We can set the grid lines while exporting data to pdf file.
            RadGrid2.ExportSettings.OpenInNewWindow = true;
            RadGrid2.ExportSettings.IgnorePaging = true;
            RadGrid2.ExportSettings.ExportOnlyData = true;
  
            RadGrid2.ExportSettings.FileName = "PDFExportDemo";
  
            //Landscape 
            RadGrid2.ExportSettings.Pdf.PageHeight = Unit.Parse("10in");
            RadGrid2.ExportSettings.Pdf.PageWidth = Unit.Parse("16in");
  
            //// 1/2 inch margins 
            RadGrid2.ExportSettings.Pdf.PageLeftMargin = Unit.Parse("0.5in");
            RadGrid2.ExportSettings.Pdf.PageRightMargin = Unit.Parse("0.5in");
            RadGrid2.ExportSettings.Pdf.PageTopMargin = Unit.Parse("0.5in");
            RadGrid2.ExportSettings.Pdf.PageBottomMargin = Unit.Parse("0.5in");
  
            isPdfExport = true;
  
          
  
            //RadGrid2.MasterTableView.PrepareExport();
            RadGrid2.MasterTableView.GetColumn("InvName").HeaderStyle.Width = Unit.Pixel(200);
            RadGrid2.MasterTableView.GetColumn("InvName").ItemStyle.Wrap = true;
  
            RadGrid2.MasterTableView.ExportToPdf();
            //RadGrid2.MasterTableView.Columns.FindByUniqueName("InvName").ItemStyle.Width = Unit.Pixel(200);
        }
  
           }
}

7 Answers, 1 is accepted

Sort by
0
Vivek
Top achievements
Rank 2
answered on 27 May 2013, 03:49 PM
Still hoping updates on this, please reply.
0
Kostadin
Telerik team
answered on 29 May 2013, 09:55 AM
Hello Vivek,

When the cell contains a table you have to set the text-align property of the nested table cells. Check out the following code snippet.
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (isExported && e.Item is GridDataItem)
        {
            GridDataItem item = e.Item as GridDataItem;
            foreach (TableCell cell in item.Cells)
            {
                if (cell.Controls.Count > 0)
                {
                    for (int i = 0; i < cell.Controls.Count; i++)
                    {
                        if (cell.Controls[i] is LiteralControl)
                        {
                            (cell.Controls[i] as LiteralControl).Text = (cell.Controls[i] as LiteralControl).Text.Replace("<td>", "<td style=\"text-align:center\">");
                        }
                    }
                }
            }
        }
 
        if (isExported && e.Item is GridHeaderItem)
        {
            foreach (TableCell cell in e.Item.Cells)
            {
                cell.Style["text-align"] = "center";               
            }
        }
    }


Regards,
Kostadin
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Vivek
Top achievements
Rank 2
answered on 29 May 2013, 10:48 AM
Hi Kostadin,

   Thanks, this solves the problem, data is getting center aligned in all the columns but I want first column data should be left aligned, not center aligned. please let me know if you have any suggestion on making first column data left aligned.
0
Vivek
Top achievements
Rank 2
answered on 31 May 2013, 09:42 AM
Hi Kostadin,

Please let me know if you have any updates on this.
0
Kostadin
Telerik team
answered on 03 Jun 2013, 07:24 AM
Hi Vivek,

A possible solution is to get the cell by column unique name and compare it with the current data item cell. Check out the following code snippet:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (isExported && e.Item is GridDataItem)
        {
            GridDataItem item = e.Item as GridDataItem;
            foreach (TableCell cell in item.Cells)
            {
                if (cell.Controls.Count > 0 && cell != item["ColumnUniqueName"])
                {
                    for (int i = 0; i < cell.Controls.Count; i++)
                    {
                        if (cell.Controls[i] is LiteralControl)
                        {
                            (cell.Controls[i] as LiteralControl).Text = (cell.Controls[i] as LiteralControl).Text.Replace("<td>", "<td style=\"text-align:center\">");
                        }
                    }
                }
            }
        }
  
        if (isExported && e.Item is GridHeaderItem)
        {
            foreach (TableCell cell in e.Item.Cells)
            {
                cell.Style["text-align"] = "center";              
            }
        }
    }



Regards,
Kostadin
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Vivek
Top achievements
Rank 2
answered on 05 Jun 2013, 08:58 AM
That helps with my formatting of the grid, however, i'd still like to stretch the first column text (means I want to set different width for the first column to fit the content).

I'm setting all the col width using below piece of code - 
e.RawHTML = e.RawHTML.Replace("<col  />", "<col style=\"width:90px;\" />");  

This works great, but the problem is - this sets the equal width for all the columns but I want different width to be set for the first column.

To achive this, I tried setting the width using "UniqueName" (as shown below) but this didn't work, first column data is still showing wraped to 90px width in the downloaded pdf file.

            RadGrid2.MasterTableView.GetColumn("InvName").HeaderStyle.Width = Unit.Pixel(150);
            RadGrid2.MasterTableView.GetColumn("InvName").ItemStyle.Width = Unit.Pixel(200);

Please let me know, how different width for the first column can be set in the above mentioned scenario?
0
Kostadin
Telerik team
answered on 10 Jun 2013, 07:07 AM
Hi Vivek,

You could set a column with by hooking on ItemCommand or button click event handler and loop through all columns and set the width of the column headers. Please check out the following code snippet.
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "ExportToPdf")
        {
            foreach (GridColumn col in RadGrid1.MasterTableView.Columns)
            {
                col.HeaderStyle.Width = Unit.Pixel(52);
            }
        }
    }


Regards,
Kostadin
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
General Discussions
Asked by
Vivek
Top achievements
Rank 2
Answers by
Vivek
Top achievements
Rank 2
Kostadin
Telerik team
Share this question
or