We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Export Sfdatagrid to Pdf with collapsed groups

Hi,

Is it possible to export sfdatagrid to pdf with collapsed and expanded groups exactly as it is in the datagrid?

thanks in advance


3 Replies 1 reply marked as answer

DM Dhanasekar Mohanraj Syncfusion Team October 18, 2022 09:14 AM

Hi Farzin Hameed,

Your requirement can be achieved by creating the custom PDF export by using DataGridToPdfConverter and overriding the ExportGroupToPdf and ExportRecordsToPdf methods in the custom PDF Export option. Please refer to the below code snippet,


public partial class Form1: Form

{

 

    DataGridToPdfConverterExt CustomPDF;

 

    public Form1()

    {

        InitializeComponent();

        //create the instance for DataGridToPdfConverterExt

        CustomPDF = new DataGridToPdfConverterExt();

 

        sfDataGrid.DataSource = new ViewModel().Orders;

        this.sfDataGrid.ShowGroupDropArea = true;

        btnExportPDF.Click += OnExportPDFClicked;       

    }

  

 

    private void OnExportPDFClicked(object sender, EventArgs e)

    {

       

       

 

        //call ExportToPdf method in the custom pdf 

      

        SaveFileDialog saveFileDialog = new SaveFileDialog

        {

            Filter = "PDF Files(*.pdf)|*.pdf"

        };

 

 

 

 

        PdfExportingOptions options = new PdfExportingOptions();

        options.RepeatHeaders = true;

        options.ExportGroupSummary = true;

        options.ExportFormat = true;

        options.FitAllColumnsInOnePage = false;

        options.ExportTableSummary = true;

       

        //options.CellExporting += OnCellExporting;

        //Set document information.

        Syncfusion.Pdf.PdfDocument document = new Syncfusion.Pdf.PdfDocument();

 

        PdfPage page = document.Pages.Add();

        RectangleF bounds = new RectangleF(0, 10, document.Pages[0].GetClientSize().Width, 50);

        PdfPageTemplateElement footer = new PdfPageTemplateElement(bounds);

        PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 7);

        PdfBrush brush = new PdfSolidBrush(Color.Black);

 

        //Create page number field.

        PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush);

 

        //Add the fields in composite fields.

        PdfCompositeField compositeField = new PdfCompositeField(font, brush, "{0}", pageNumber);

        compositeField.Bounds = footer.Bounds;

 

        //Draw the composite field in footer.

        compositeField.Draw(footer.Graphics, new PointF(250, 40));

 

        //Add the footer template at the bottom.

        document.Template.Bottom = footer;

 

        var pdfGrid = CustomPDF.ExportToPdfGrid(sfDataGrid ,sfDataGrid.View, options);

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

 

 

 

 

 

        if (saveFileDialog.ShowDialog() == DialogResult.OK)

        {

            using (Stream stream = saveFileDialog.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", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)

            {

                //Launching the Pdf file using the default Application.

                System.Diagnostics.Process.Start(saveFileDialog.FileName);

            }

        }

    }


C# Code Snippet related to DataGridToPdfConverter Customization:

//Customize the DataGridToPdfConverter

public class DataGridToPdfConverterExt : DataGridToPdfConverter

{

    public override PdfDocument ExportToPdf(SfDataGrid sfgrid)

    {

        return base.ExportToPdf(sfgrid);

    }

 

    public override PdfDocument ExportToPdf(SfDataGrid sfgrid, ICollectionViewAdv view, PdfExportingOptions pdfExportingOptions)

    {

        return base.ExportToPdf(sfgrid, view, pdfExportingOptions);

    }

    public override PdfGrid ExportToPdfGrid(SfDataGrid sfgrid, ICollectionViewAdv view, PdfExportingOptions pdfExportingOptions)

    {

        return base.ExportToPdfGrid (sfgrid, view, pdfExportingOptions);

    }

 

    public override PdfDocument ExportToPdf(SfDataGrid sfgrid, PdfExportingOptions pdfExportingOptions)

    {

        return base.ExportToPdf(sfgrid, pdfExportingOptions);

    }

 

    protected override void ExportRecordsToPdf(SfDataGrid sfgrid, PdfGrid pdfGrid, PdfExportingOptions pdfExportingOptions, IEnumerable records, IPropertyAccessProvider propertyAccessProvider, Group group)

    {

        bool hasrecords = false;

        foreach (var rec in records)

        {

            hasrecords = true;

            break;

        }

 

        if (!hasrecords)

            return;

 

        //here skip the collapse records while exporting to PDF

        //here adds the null condition to avoid the exception

        if (group != null && !group.IsExpanded)

            return;

 

        foreach (var rec in records)

        {

            var record = (rec is RecordEntry) ? (rec as RecordEntry).Data : rec;

            if (record == null)

                continue;

 

            pdfExportingOptions.RowIndex++;

           

            ExportRecordToPdf(sfgrid, pdfGrid, pdfExportingOptions, record, propertyAccessProvider, group);

 

            ExportDetailsViewToPDF(sfgrid, pdfGrid, pdfExportingOptions, rec, propertyAccessProvider, group);

 

            pdfExportingOptions.RowIndex += sfgrid.DetailsViewDefinitions.Count;

        }

    }

 

    protected override void ExportGroupToPdf(SfDataGrid sfgrid, PdfGrid pdfGrid, ICollectionViewAdv view, PdfExportingOptions pdfExportingOptions, Group group)

    {

        var propertyAccessProvider = view.GetPropertyAccessProvider();

        if (group.IsGroups)

        {

            pdfExportingOptions.RowIndex++;

            ExportGroupCaptionToPdf(sfgrid, pdfGrid, view, pdfExportingOptions, group);

            if (!group.IsBottomLevel)

            {

                if (group.IsExpanded)

                {

                    foreach (Group childGroup in group.Groups)

                    {

                        if (childGroup.ItemsCount > 0)

                            ExportGroupToPdf(sfgrid, pdfGrid, view, pdfExportingOptions, childGroup);

                    }

                }

            }

            else

            {

                IEnumerable records = null;

                if (group.Records is VirtualRecordEntryList)

                    records = group.Records.GetSource();

                else

                    records = group.Records;

                ExportRecordsToPdf(sfgrid, pdfGrid, pdfExportingOptions, records, propertyAccessProvider, group);

                if (pdfExportingOptions.ExportGroupSummary)

                    ExportSummariesToPdf(sfgrid, pdfGrid, view, pdfExportingOptions, group);

            }

        }

    }

}


Please find the sample in the attachment and let us know if you have any concerns about this.


Regards,

Dhanasekar M.

If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly


Marked as answer

FH Farzin Hameed October 20, 2022 12:37 AM

This is perfect! Thank You!



DM Dhanasekar Mohanraj Syncfusion Team October 20, 2022 08:13 AM

Hi Farzin Hameed,


If you are satisfied with our response, please mark it as an answer. Otherwise, please let us know if you have any further queries on this. We are happy to help you.


Regards,

Dhanasekar M.


Loader.
Live Chat Icon For mobile
Up arrow icon