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

Export RadGrid into PDF

9 Answers 578 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Yuriy Shvadskiy
Top achievements
Rank 1
Yuriy Shvadskiy asked on 02 Jul 2009, 10:51 AM

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

9 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 07 Jul 2009, 04:59 PM
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.
0
LeBear
Top achievements
Rank 1
answered on 26 Aug 2009, 06:53 PM
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.
0
Daniel
Telerik team
answered on 26 Aug 2009, 08:14 PM
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.
0
kernelk
Top achievements
Rank 1
answered on 27 Aug 2013, 09:02 PM
Thank you, Daniel. Finally got formatting working. Thanks for the explanation.
0
Ranjith
Top achievements
Rank 1
answered on 01 Jan 2014, 09:15 AM
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
0
Kostadin
Telerik team
answered on 06 Jan 2014, 09:38 AM
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.
0
Hamed
Top achievements
Rank 1
answered on 18 Jan 2021, 10:30 AM

please help me

See the attachment pdf is exporting like this...can you resolve this issue.

Thank you

0
Hamed
Top achievements
Rank 1
answered on 18 Jan 2021, 10:32 AM
please help me
See the attachment when i was exporting grid data into pdf so it looks like this...can you resolve this issue.
Thank you
0
Vessy
Telerik team
answered on 20 Jan 2021, 01:39 PM

Hi Hamed,

Currently, the Grid is able to export to PDF only the styles which are applied inline to its elements. If you are styling them via CSS selectors (classes, tags, etc), the applied styles will not be exported.

You can see a detailed list with all unsupported by the PDF export features here:

https://docs.telerik.com/devtools/aspnet-ajax/controls/editor/functionality/import-and-export/export-to-pdf#unsupported-features-and-scenarios

Regards,
Vessy
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Grid
Asked by
Yuriy Shvadskiy
Top achievements
Rank 1
Answers by
Daniel
Telerik team
LeBear
Top achievements
Rank 1
kernelk
Top achievements
Rank 1
Ranjith
Top achievements
Rank 1
Kostadin
Telerik team
Hamed
Top achievements
Rank 1
Vessy
Telerik team
Share this question
or