Ejemplo n.º 1
0
        public void ExportToPdf(SfDataGrid dataGrid)
        {
            if (dataGrid == null)
            {
                return;
            }
            try
            {
                var options = new PdfExportingOptions();
                options.CellsExportingEventHandler   = GridCellPdfExportingEventhandler;
                options.ExportingEventHandler        = GridPdfExportingEventhandler;
                options.PageHeaderFooterEventHandler = PdfHeaderFooterEventHandler;
                options.ExportStackedHeaders         = true;
                var          document = dataGrid.ExportToPdf(options);
                MemoryStream stream   = new MemoryStream();
                document.Save(stream);
                PdfViewerControl pdfViewer = new PdfViewerControl();
                pdfViewer.Load(stream);
                Window window = new Window();
                window.Content = pdfViewer;
                window.Show();

                //var document = new PdfDocument();
                //document.PageSettings.Orientation = PdfPageOrientation.Landscape;
                //document.PageSettings.SetMargins(20);
                //var page = document.Pages.Add();
                //var pdfGrid = dataGrid.ExportToPdfGrid(dataGrid.View, options);

                //var format = new PdfGridLayoutFormat()
                //{
                //    Layout = PdfLayoutType.Paginate,
                //    Break = PdfLayoutBreakType.FitPage
                //};

                //pdfGrid.Draw(page, new PointF(), format);


                //SaveFileDialog sfd = new SaveFileDialog
                //{
                //    Filter = "PDF Files(*.pdf)|*.pdf"
                //};

                //if (sfd.ShowDialog() == true)
                //{
                //    using (Stream stream = sfd.OpenFile())
                //    {
                //        document.Save(stream);
                //    }

                //    //Message box confirmation to view the created Pdf file.
                //    if (MessageBox.Show("Do you want to view the Pdf file?", "Pdf file has been created",
                //                        MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes)
                //    {
                //        //Launching the Pdf file using the default Application.
                //        System.Diagnostics.Process.Start(sfd.FileName);
                //    }
                //}
            }
            catch (Exception)
            {
            }
        }