Export to pdf without collapsed row

Hi,


I Have simple grid:



And after export to pdf:



It's possible to export grid as is? I mean without collapsed/unvisible rows.



5 Replies 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team September 26, 2022 06:06 PM UTC

Hi Michal Dziubek,

Your requirement can be achieved by create the custom PDF export by using DataGridToPdfConverter and override the ExportGroupToPdf and ExportRecordsToPdf methods in 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;

         sfDataGrid.ShowGroupDropArea = true;

         btnExportPDF.Click += OnExportPDFClicked;       

     }   

 

     private void OnExportPDFClicked(object sender, EventArgs e)

     {

         PdfExportingOptions options = new PdfExportingOptions();      

 

         //call ExportToPdf method in the custom pdf 

         var document = CustomPDF.ExportToPdf(sfDataGrid, options);

 

         SaveFileDialog saveFileDialog = new SaveFileDialog

         {

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

         };

         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 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

          if (!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)

              {

                  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,

Vijayarasan S

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



Attachment: SfDataGridDemo_542631ba.zip

Marked as answer

MD Michal Dziubek September 28, 2022 01:34 PM UTC


Works exactly as I meant. One more question:


My grid has bold text on summary lines and is right-aligned. During the export, the font changes to normal and the alignment is left. Is it possible to keep these settings during the export?


Grid:



PDF:





VS Vijayarasan Sivanandham Syncfusion Team September 29, 2022 02:04 PM UTC

Hi Michal Dziubek,

Your requirement to format the cell while exporting into PDF in SfDataGrid can be achieved by using the CellExporting event of the PdfExportingOption. Please refer to the below code snippet,


PdfExportingOptions options = new PdfExportingOptions();

//Event subscription

options.CellExporting += OnCellExporting;

//call ExportToPdf method in the custom pdf 

var document = CustomPDF.ExportToPdf(sfDataGrid, options);

document.Save("Sample.pdf");

 

//Event customization

void OnCellExporting(object sender, DataGridCellPdfExportingEventArgs e)

{

    //here customize based on your scenario

    if (e.CellType == ExportCellType.GroupCaptionCell)

    {

        //here customizes the caption summary row when export into pdf

        //here changes the font style when export into PDF

        e.PdfGridCell.Style.Font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold));

    }

}


UG Link: https://help.syncfusion.com/windowsforms/datagrid/exporttopdf?cs-save-lang=1&cs-lang=csharp#embedding-fonts-in-pdf-file

https://help.syncfusion.com/windowsforms/datagrid/exporttopdf?cs-save-lang=1&cs-lang=csharp#changing-row-style-in-pdf-based-on-data

https://help.syncfusion.com/windowsforms/datagrid/exporttopdf?cs-save-lang=1&cs-lang=csharp#exporting-middle-eastern-languages-arabic-hebrew-from-sfdatagrid-to-pdf

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

Regards,
Vijayarasan S

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


Attachment: ModifiedSample_fabcfb58.zip


MD Michal Dziubek October 5, 2022 01:52 PM UTC

I want add page number.


So I modfied DataGridToPdfConverterExt and added


        public override PdfGrid ExportToPdfGrid(SfDataGrid sfgrid, ICollectionViewAdv view, PdfExportingOptions pdfExportingOptions)
{
return base.ExportToPdfGrid (sfgrid, view, pdfExportingOptions);
}

Changing view of grid on load:


 GridTableSummaryRow tsr = new GridTableSummaryRow();

tsr.Name = "nn";
tsr.ShowSummaryInRow = false;
tsr.Position=VerticalPosition.Top ;




GridSummaryColumn summaryColumn1 = new GridSummaryColumn();
summaryColumn1.Name = "Column1";
summaryColumn1.SummaryType = SummaryType.DoubleAggregate;
summaryColumn1.Format = "{Count}";
summaryColumn1.MappingName = "Country";




tsr.SummaryColumns.Add(summaryColumn1);
this.sfDataGrid.TableSummaryRows.Add(tsr);



in void ExportRecordsToPdf


And I recived System.NullReferenceException in line:


if (!group.IsExpanded)

Why?


I attached sample


Attachment: SfDataGridDemo_126a59b5.zip



VS Vijayarasan Sivanandham Syncfusion Team October 6, 2022 02:21 PM UTC

Hi Michal Dziubek,

The reported problem occurs due to records not being grouped in SfDataGrid while exporting to PDF. We have not added the null check for this case. So, the null reference exception occurs when exporting to PDF.

However, you can resolve the reported problem by checking the null condition for the group. Please refer to the below code snippet,

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

{           

 

            //here skip the collapse records while exporting to PDF

            //here adds the null condition to avoid the exception

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

                return;

}


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


Regards,

Vijayarasan S


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


Attachment: ModifiedSample_8bf19c08.zip

Loader.
Up arrow icon