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

export hierarchical grid to pdf

4 Answers 107 Views
GridView
This is a migrated thread and some comments may be shown as answers.
jen
Top achievements
Rank 1
jen asked on 25 May 2016, 05:23 PM

I need to export a hierarchical grid to pdf, but I can't seem to get the child data into the pdf.

I found examples for using the ElementExporting and ElementExported events, but when I call .ExportToPdf() those events do not get thrown.
http://www.telerik.com/forums/export-hierarchical-grid-b2daf443a4fc

Some threads also mention following this article to make a Table, but I can't figure out what making this table has to do with ExportToPdf.
http://docs.telerik.com/devtools/silverlight/controls/radrichtextbox/document-elements/features-tables.html


Code:
My grid's HierarchyChildTemplate is defined in the View's resources.

        private void Save()
        {
            View.grd_Results.ElementExporting -= this.ElementExporting;
            View.grd_Results.ElementExporting += this.ElementExporting;

            View.grd_Results.ElementExported -= this.ElementExported;
            View.grd_Results.ElementExported += this.ElementExported;

            string extension = "pdf";

            SaveFileDialog dialog = new SaveFileDialog()
            {
                DefaultExt = extension,
                Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, "Pdf"),
                FilterIndex = 1
            };

            if (dialog.ShowDialog() == true)
            {
                GridViewPdfExportOptions opt = new Telerik.Windows.Controls.GridViewPdfExportOptions()
                {
                    ShowColumnFooters = false,
                    ShowColumnHeaders = true,
                    ShowGroupFooters = true,
                    PageOrientation = PageOrientation.Portrait,
                    ExportDefaultStyles = true,
                    AutoFitColumnsWidth = true,
                    Items = Results
                };

                using (Stream stream = dialog.OpenFile())
                {
                     View.grd_Results.ExportToPdf(stream, opt); //ElementExporting && ElementExported do not get thrown
                }

                View.busy_PDFView.IsBusy = false;
            }
        }

        private void ElementExporting(object sender, GridViewElementExportingEventArgs e)
        {
            if (e.Element == ExportElement.HeaderRow)
            {
                var htmlVisualExportParameters = e.VisualParameters as GridViewHtmlVisualExportParameters;
                if (htmlVisualExportParameters != null)
                {
                    htmlVisualExportParameters.FontWeight = FontWeights.Bold;
                }
            }
        }

        private void ElementExported(object sender, GridViewElementExportedEventArgs e)
        {
            if (e.Element == ExportElement.Row)
            {
                IncentiveUnitMock obj = e.Context as IncentiveUnitMock;

                var template = View.LayoutRoot.Resources["ChildTableTemplate"] as DataTemplate;
                var grid = template.LoadContent() as RadGridView;

                grid.DataContext = obj;

                GridViewPdfExportOptions opt = new GridViewPdfExportOptions()
                {
                    ShowColumnFooters = false,
                    ShowColumnHeaders = true,
                    ShowGroupFooters = true,
                    PageOrientation = PageOrientation.Portrait,
                    ExportDefaultStyles = true,
                    AutoFitColumnsWidth = true,
                    Items = obj.Incentives
                };

                var stream = new MemoryStream();
                grid.ExportToPdf(stream, opt);

                var subExport = new StreamReader(stream).ReadToEnd();

                e.Writer.Write(subExport);

            }
        }

  

How can I export the full table and child tables to pdf?

4 Answers, 1 is accepted

Sort by
0
jen
Top achievements
Rank 1
answered on 26 May 2016, 02:32 PM

I've discovered that ExportToPdf() throws the ElementExportingToDocument and ElementExportedToDocument events instead.

I can use these events in a similar way, except GridViewElementExportedToDocumentEventArgs does not have a StreamWriter.
This code will not work:

                var stream = new MemoryStream();
                grid.ExportToPdf(stream, opt);
                var subExport = new StreamReader(stream).ReadToEnd();
                e.Writer.Write(subExport); //e.Writer does not exist

 

how should I add the child grid export to the main export?

0
Dilyan Traykov
Telerik team
answered on 27 May 2016, 10:50 AM
Hello Jen,

I'm afraid that currently there is no straightforward way to export a hierarchical RadGridView to pdf. You can, however, modify the content of the exported document with the new ExportToWorkbook and ExportToRadFixedDocument methods. ExportToRadFixedDocument method utilizes the PdfProcessing library and ExportToWorkbook method utilizes the SpreadProcessing library. You can check their documentation for more information on how to modify them.

You could also give the approach about exporting to CSV suggested by one of my colleagues in this forum thread a try and see if it works for you. Here is a similar thread, regarding the export to HTML.

I hope you find this information helpful.

Regards,
Dilyan Traykov
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
jen
Top achievements
Rank 1
answered on 27 May 2016, 03:37 PM

I found a way easier solution. I use the code below to 'screenshot' the grid as-is and send that image to the printer. Then we can print to pdf. Maybe others will find this useful too.

public class PrintScreenTool
 {
        public static PrintDocument PrintDoc(UIElement element)
        {
            var document = new PrintDocument();
            document.PrintPage += (s, args) =>
            {
                args.PageVisual = element;
            };
            return document;
        }


        public static Image PrintScreen(UIElement element)
        {
            WriteableBitmap writeableBitmap = new WriteableBitmap(element, null);
            writeableBitmap.Invalidate();
            Image img = new Image();
            img.Source = writeableBitmap;
            return img;
        }

}

 

       private void PrintResults()
        {
            //expand
            View.grd_Results.ExpandAllHierarchyItems();

            var image = PrintScreenTool.PrintScreen(View.grd_Results);
            var document = PrintScreenTool.PrintDoc(image);
            document.Print("Report_" + DateTime.Now.ToString(("dd_MM_yyyy")));

        }

0
Dilyan Traykov
Telerik team
answered on 30 May 2016, 08:09 AM
Hello Jen,

I'm glad to hear that you found a solution that suits your requirements. I would also like to thank you for sharing it with the community.

Regards,
Dilyan Traykov
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
GridView
Asked by
jen
Top achievements
Rank 1
Answers by
jen
Top achievements
Rank 1
Dilyan Traykov
Telerik team
Share this question
or