Export RadGrid into PDF

7 posts, 0 answers
  1. Yuriy Shvadskiy
    Yuriy Shvadskiy avatar
    1 posts
    Member since:
    Jul 2009

    Posted 02 Jul 2009 Link to this post

    I am trying to Export into PDF from RadGrid. My grid is too wide on a screen and when output to PDF happened it is squished. I was trying to apply style before I do Export but it does not make any difference.

            private void btnExportToPDF_Click(object sender, EventArgs e)

            {

                ApplyStylesToPDFExport(giftGrid.MasterTableView);

                giftGrid.MasterTableView.ExportToPdf();

            }

     

            private void ApplyStylesToPDFExport(GridTableView view)

            {

                // Get access to the header of the grid

                GridItem headerItem = view.GetItems(GridItemType.Header)[0];

     

                // Apply some css style to the header

                foreach (TableCell cell in headerItem.Cells)

                {

                    cell.Style["font-family"] = "Verdana";

                    cell.Style["text-align"] = "center";

                    cell.Style["vertical-align"] = "middle";

                    cell.Style["font-size"] = "2px";

                }

                           

                // Get access to the date of the grid

                GridItem[] dataItems = view.GetItems(GridItemType.Item);

     

                // Apply some css style to the data items

                foreach (GridItem item in dataItems)

                {

                    foreach (TableCell cell in item.Cells)

                    {

                        cell.Style["font-family"] = "Verdana";

                        cell.Style["text-align"] = "center";

                        cell.Style["vertical-align"] = "middle";

                        cell.Style["font-size"] = "2px";

                    }

                }

            }

    Please explain to me what do I do wrong.

    Thanks,

    Yuriy

  2. Daniel
    Admin
    Daniel avatar
    4945 posts

    Posted 07 Jul 2009 Link to this post

    Hello Yuriy,

    For your convenience I created a sample project that demonstrates how to attain the desired behavior.

    I hope this helps.

    Regards,
    Daniel
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. LeBear
    LeBear avatar
    233 posts
    Member since:
    May 2006

    Posted 26 Aug 2009 Link to this post

    Perhaps I'm missing something, but the attachment is virtually the same as the code provided by Yuriy.

    I'm trying this, and it doesn't affect the PDF output in any way.  I have successfully altered the column width prior to export, but I can't affect any fonts in the output.  Interestingly, I put this code in PreRender without any conditions, and it does affect the output to the screen.  I can then export to PDF, and that exported content remains unaffected.
  5. Daniel
    Admin
    Daniel avatar
    4945 posts

    Posted 26 Aug 2009 Link to this post

    Hello Barry,

    The attached sample project works properly. However it won't work, if you set IgnorePaging="true" or if you rebind RadGrid before export. In such cases you should apply your custom styles on ItemCreated as shown below:

    Button click handler:
    bool IsExport = false
    protected void Button6_Click(object sender, EventArgs e) 
        IsExport = true
        RadGrid1.MasterTableView.ExportToPdf(); 

    ItemCreated:
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
        if (IsExport) 
            ApplyStylesToPDFExport(e.Item); 

    Sample code:
    private void ApplyStylesToPDFExport(GridItem item) 
        if (item is GridHeaderItem) 
            foreach (TableCell cell in item.Cells) 
            { 
                cell.Style["font-family"] = "Verdana"
                cell.Style["text-align"] = "left"
                cell.Style["font-size"] = "6pt"
            } 
        if (item is GridDataItem) 
        { 
            item.Style["font-size"] = "6px"
            item.Style["background-color"] = item.ItemType == GridItemType.AlternatingItem ? "#DDDDDD" : "#AAAAAA"
        } 

    Best regards,
    Daniel
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  6. kernelk
    kernelk avatar
    15 posts
    Member since:
    Aug 2012

    Posted 27 Aug 2013 Link to this post

    Thank you, Daniel. Finally got formatting working. Thanks for the explanation.
  7. Ranjith
    Ranjith avatar
    6 posts
    Member since:
    Jan 2014

    Posted 01 Jan 2014 Link to this post

    Hi,

    I am trying to export PDF from my RadGrid and its exporting successfully. But i am facing some design issues in the exported PDF file.

    1. All the text are coming with Hyperlinks in the exported PDF.
    2. Set the Forecolor and width, which are not getting reflected in the exported PDF.

    Below is my c# code 

    protected void btnExportToPDF_Click(object sender, ImageClickEventArgs e)
            {
                ApplyStylesToPDFExport(RadGrid1.MasterTableView);
                RadGrid1.ExportSettings.OpenInNewWindow = true;
                RadGrid1.ExportSettings.IgnorePaging = true;
                RadGrid1.ExportSettings.FileName = "Test";
                RadGrid1.MasterTableView.ExportToPdf();
            }
    private void ApplyStylesToPDFExport(GridTableView view)
            {
                GridItem headerItem = view.GetItems(GridItemType.Header)[0];
                foreach (TableCell cell in headerItem.Cells)
                {
    switch (cell.Text)
                    {
                        case "A":
                            cell.Width = Unit.Pixel(5); // Tried like this. Not reflecting
                            break;
                        case "B":
                            cell.Style["width"] = "15px"; // Tried like this. Not reflecting
                            break;
    }
                    cell.Style["font-family"] = "Verdana";
                    cell.Style["font-bold"] = "true";
                    cell.Style["text-align"] = "left";
                    cell.Style["vertical-align"] = "middle";
                    cell.Style["font-size"] = "8px";
                    cell.ForeColor = System.Drawing.Color.Black; // Tried like this. Not reflecting

                }
                GridItem[] dataItems = view.GetItems(GridItemType.Item);
                foreach (GridItem item in dataItems)
                {
                    foreach (TableCell cell in item.Cells)
                    {
                        cell.Style["font-family"] = "Verdana";
                        cell.Style["text-align"] = "left";
                        cell.Style["vertical-align"] = "left";
                        cell.Style["font-size"] = "6px";
                        cell.Style["text-decoration"] = "none"; // Tried like this. Not reflecting

                        cell.Style["ForeColor"] = "#000";
                    }
                }
                dataItems = view.GetItems(GridItemType.AlternatingItem);
                foreach (GridItem item in dataItems)
                {
                    foreach (TableCell cell in item.Cells)
                    {
                        cell.Style["font-family"] = "Verdana";
                        cell.Style["text-align"] = "left";
                        cell.Style["vertical-align"] = "middle";
                        cell.Style["font-size"] = "6px";
                        cell.Style["text-decoration"] = "none";
                        cell.ForeColor = System.Drawing.Color.Black;
                    }
                }
            }

    Please advise.

    Thanks
    Ranjith
  8. Kostadin
    Admin
    Kostadin avatar
    1711 posts

    Posted 06 Jan 2014 Link to this post

    Hello Ranjith,

    In order to remove the hyperlink text you have to set ExportOnlyData to true. Regards the styling issue you have to specify the width of the column instead setting a width of a single cell. Also instead setting a ForeColor you could set a color attribute. Please check out the following code snippet.
    cell.Style["color"] = "black";

    I would recommend you to review the following live example which demonstrates how to style the grid when exporting to PDF.


    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017