RadGrid with ExportToPdf, formatting question

2 Answers 96 Views
Grid PdfViewer
Nancy
Top achievements
Rank 1
Iron
Nancy asked on 22 Jul 2023, 12:02 AM

I am trying to use ExportToPdf to generate a .pdf document from a RadGrid.  Here's a simplified version of my grid.  The labels export to the .pdf, but the table with "TGIF" does not.  I need to use HTML formatting to arrange the labels.  What am I doing wrong?  Thanks!

        <telerik:RadGrid ID="gridTest" runat="server" AutoGenerateColumns="False" 

            Skin="Default" Width="800px"             
            OnItemCreated="gridTest_ItemCreated" >

            <ExportSettings 
                IgnorePaging="true" OpenInNewWindow="true" ExportOnlyData="False">
                <Pdf PaperSize="Letter"></Pdf>
            </ExportSettings>

            <MasterTableView ShowHeader="false">                 
                <Columns>
                    <telerik:GridTemplateColumn DataField="strCaseID" UniqueName="strCaseID" ShowFilterIcon="false" >
                        <ItemTemplate>

                            <table>
                                <tr>
                                    <td>
                                        TGIF!
                                    </td>
                                </tr>
                            </table>
                  
                            <asp:Label ID="lbName"                 runat="server" Text='<%#Eval("strEmployeeFName")%>'                ></asp:Label><br />
                            <asp:Label ID="lbAddressStreet1"       runat="server" Text='<%#Eval("strEmployeeAddressStreet1")%>'       ></asp:Label><br />
                            <asp:Label ID="lbAddressStreet2"       runat="server" Text='<%#Eval("strEmployeeAddressStreet2")%>'       ></asp:Label><br />
                            <asp:Label ID="lbAddressCity"          runat="server" Text='<%#Eval("strEmployeeAddressCity")%>'          ></asp:Label><br />
                            <asp:Label ID="lbAddressState"         runat="server" Text='<%#Eval("strEmployeeAddressState")%>'         ></asp:Label><br />
                            <asp:Label ID="lbAddressZip"           runat="server" Text='<%#Eval("strEmployeeAddressZip")%>'           ></asp:Label><br />

                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
            </MasterTableView>

        </telerik:RadGrid>
		
        in Page_Load...
		
		    PopulateGrid(gridTest);
            ConfigureGridBeforeExportToPdf();
            gridTest.MasterTableView.ExportToPdf();

        protected void gridTest_ItemCreated(object sender, GridItemEventArgs e)
        {
            try
            {
                ConfigureGridItem(e.Item);
            }
            catch (Exception ex)
            {
                // do stuff
            }
        }

        protected void ConfigureGridItem(GridItem item)
        {
            // ***** THIS IS FOR - each Item

            item.Style["color"] = "purple"; // font color

            if (item is GridHeaderItem)
            {
                item.Visible = false;
            }
            else if (item is GridDataItem)
            {
                item.Style["vertical-align"] = "middle";
                item.Style["text-align"] = "left";
            }

            switch (item.ItemType)
            {
                case GridItemType.Item: item.Style["background-color"] = "lightyellow"; break;
                case GridItemType.AlternatingItem: item.Style["background-color"] = "mistyrose"; break;
            }
        }

        public void ConfigureGridBeforeExportToPdf()
        {
            // ***** THIS IS FOR - entire Grid

            // format - Pdf Settings
            gridTest.ExportSettings.Pdf.BorderType = GridPdfSettings.GridPdfBorderType.OuterBorders;
            gridTest.ExportSettings.Pdf.BorderStyle = GridPdfSettings.GridPdfBorderStyle.Thick;
            gridTest.ExportSettings.Pdf.BorderColor = System.Drawing.Color.HotPink;
            gridTest.ExportSettings.Pdf.PaperSize = GridPaperSize.Letter;
            gridTest.ExportSettings.Pdf.PageHeight = Unit.Parse("162mm");
            gridTest.ExportSettings.Pdf.PageWidth = Unit.Parse("600mm");

            // format - header
            GridItem headerItem = gridTest.MasterTableView.GetItems(GridItemType.Header)[0];
            headerItem.Visible = false;

            // format - footer
            gridTest.ExportSettings.Pdf.PageFooter.MiddleCell.Text = "<?page-number?>";
            gridTest.ExportSettings.Pdf.PageFooter.MiddleCell.TextAlign = GridPdfPageHeaderFooterCell.CellTextAlign.Center;
        }		
		

2 Answers, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 25 Jul 2023, 09:30 AM

Hello Nancy,

There are a few requirements when it comes to exporting HTML tables. Can you try the suggestion from the Exporting HTML tables documentation article and see if that works?

Regards,
Attila Antal
Progress Telerik

Heads up! Telerik UI for ASP.NET AJAX versions for .NET 3.5 and 4.0 are retired. Progress will continue shipping assemblies compatible with .NET 4.5 and later. See whether this affects your apps in this article.
0
Neha
Top achievements
Rank 2
Iron
Iron
answered on 30 Jul 2023, 10:34 PM

You can try simplifying the content inside the <ItemTemplate> to make it more compatible with the export process. One approach is to use a single <asp:Label> for the TGIF! message and use the Text property to set the content dynamically. Here's an updated version of the <ItemTemplate>

<asp:Label ID="lbTGIF" runat="server" Text="TGIF!" CssClass="tgif-label"></asp:Label><br />
<asp:Label ID="lbName" runat="server" Text='<%#Eval("strEmployeeFName")%>'></asp:Label><br />
<asp:Label ID="lbAddressStreet1" runat="server" Text='<%#Eval("strEmployeeAddressStreet1")%>'></asp:Label><br />
<asp:Label ID="lbAddressStreet2" runat="server" Text='<%#Eval("strEmployeeAddressStreet2")%>'></asp:Label><br />
<asp:Label ID="lbAddressCity" runat="server" Text='<%#Eval("strEmployeeAddressCity")%>'></asp:Label><br />
<asp:Label ID="lbAddressState" runat="server" Text='<%#Eval("strEmployeeAddressState")%>'></asp:Label><br />
<asp:Label ID="lbAddressZip" runat="server" Text='<%#Eval("strEmployeeAddressZip")%>'></asp:Label><br />

Next, you can define a CSS class for the TGIF! label to set the desired formatting.

<style>
    .tgif-label {
        font-weight: bold;
        font-size: 18px;
        color: purple;
    }
</style>
By using a single <asp:Label> for the TGIF! message, the content inside the <ItemTemplate> becomes simpler and more likely to be successfully exported to the PDF.
Tags
Grid PdfViewer
Asked by
Nancy
Top achievements
Rank 1
Iron
Answers by
Attila Antal
Telerik team
Neha
Top achievements
Rank 2
Iron
Iron
Share this question
or