Listen
Copied RSS Feed

.NET MAUI

Exporting DataGrid to PDF Made Easy in .NET MAUI

TL;DR: Learn to export your grid data to a PDF file using the Syncfusion .NET MAUI DataGrid control with custom export options like excluding columns and headers, exporting columns to a single page, and applying styles and borders.

Syncfusion .NET MAUI DataGrid control is used to display and manipulate data in a tabular view. Its rich feature set includes different column types, sorting, autofit columns and rows, and styling all elements.

This blog will explore how to export the .NET MAUI DataGrid to a PDF document effortlessly.

Note: Before proceeding, please refer to the .NET MAUI DataGrid’s getting started documentation.

Export .NET MAUI DataGrid to PDF

The Syncfusion.Maui.DataGridExport NuGet package offers PDF exporting functionalities. It allows us to utilize PDF packages to export the DataGrid into PDF formats.

Include the Syncfusion.Maui.DataGridExport package in your application from NuGet Gallery.

Then, add the .NET MAUI DataGrid control and the Export To PDF button in your xaml page. Refer to the following code example.

<StackLayout>
 <Button Text="Export To PDF" Margin="10, 10, 10, 5"
         Clicked="ExportToPdf_Clicked" />
 <syncfusion:SfDataGrid x:Name="dataGrid"
                        Margin="20"
                        VerticalOptions="FillAndExpand"
                        ItemsSource="{Binding OrderInfoCollection}"
                        GridLinesVisibility="Both"
                        HeaderGridLinesVisibility="Both"
                        AutoGenerateColumnsMode="None"
                        SelectionMode="Multiple"
                        ColumnWidthMode="Auto">
  <syncfusion:SfDataGrid.Columns>
   <syncfusion:DataGridNumericColumn Format="D"
                                     HeaderText="Order ID"
                                     MappingName="OrderID">
   </syncfusion:DataGridNumericColumn>
   <syncfusion:DataGridTextColumn HeaderText="Customer ID"
                                  MappingName="CustomerID">
   </syncfusion:DataGridTextColumn>
   <syncfusion:DataGridTextColumn MappingName="Customer"
                                  HeaderText="Customer">
   </syncfusion:DataGridTextColumn>
   <syncfusion:DataGridTextColumn HeaderText="Ship City"
                                  MappingName="ShipCity">
   </syncfusion:DataGridTextColumn>
   <syncfusion:DataGridTextColumn HeaderText="Ship Country"
                                  MappingName="ShipCountry">
   </syncfusion:DataGridTextColumn>
  </syncfusion:SfDataGrid.Columns>
 </syncfusion:SfDataGrid>
</StackLayout>

The DataGridPdfExportingController class provides all PDF exporting methods. Using the ExportToPdf or ExportToPdfGrid method, you can easily export the DataGrid content to a PDF document.

Refer to the following code example.

private void ExportToPdf_Clicked(object sender, EventArgs e)
{
    MemoryStream stream = new MemoryStream();
    DataGridPdfExportingController pdfExport = new DataGridPdfExportingController();
    DataGridPdfExportingOption option = new DataGridPdfExportingOption();
    var pdfDoc = pdfExport.ExportToPdf(this.dataGrid, option);
    pdfDoc.Save(stream);
    pdfDoc.Close(true);
    SaveService saveService = new();
    saveService.SaveAndView("ExportFeature.pdf", "application/pdf", stream);
}

You can use the SaveService class to save the PDF document at a specific location and view it on your machine.

Refer to the following image.

Exporting .NET MAUI DataGrid data to PDF

PDF exporting with customization

You can also customize the exported PDF document using the DataGridPdfExportingOption.

Exclude specific columns in the exported PDF

All columns, including hidden ones, are exported to the PDF document by default.

You can also exclude specific columns from the DataGrid when exporting to a PDF document. To do so, add the column names to the DataGridPdfExportingOption.ExcludeColumns list.

Refer to the following code example.

private void ExportToPdf_Clicked(object sender, EventArgs e)
{
    MemoryStream stream = new MemoryStream();
    DataGridPdfExportingController pdfExport = new DataGridPdfExportingController();
    DataGridPdfExportingOption option = new DataGridPdfExportingOption();
    var list = new List<string>();
    list.Add("OrderID");
    list.Add("CustomerID");
    option.ExcludedColumns = list;
    var pdfDoc = pdfExport.ExportToPdf(this.dataGrid, option);
    pdfDoc.Save(stream);
    pdfDoc.Close(true);
    SaveService saveService = new();
    saveService.SaveAndView("Export Feature.pdf", "application/pdf", stream);
}

Refer to the following image.

Excluding specific columns in .NET MAUI DataGrid while exporting to a PDF

Export all the columns on one page

When exporting DataGrid to PDF, you can fit all columns on one page. This ensures that all columns are displayed within the confines of a single page in the PDF document.

Refer to the following code example.

private void ExportToPdf_Clicked(object sender, EventArgs e)
{
    MemoryStream stream = new MemoryStream();
    DataGridPdfExportingController pdfExport = new DataGridPdfExportingController();
    DataGridPdfExportingOption option = new DataGridPdfExportingOption();
    option.CanFitAllColumnsInOnePage = true;
    var pdfDoc = pdfExport.ExportToPdf(this.dataGrid, option);
    pdfDoc.Save(stream);
    pdfDoc.Close(true);
    SaveService saveService = new();
    saveService.SaveAndView("Export Feature.pdf", "application/pdf", stream);
}

Refer to the following image.

Exporting all columns in the .NET MAUI DataGrid to a single page in a PDF

Export without header

The .NET MAUI DataGrid can be exported to a PDF document with or without a header. By default, the header is included in the exported PDF document.

Refer to the following code example.

private void ExportToPdf_Clicked(object sender, EventArgs e)
{
    MemoryStream stream = new MemoryStream();
    DataGridPdfExportingController pdfExport = new DataGridPdfExportingController();
    DataGridPdfExportingOption option = new DataGridPdfExportingOption();
    option.CanExportHeader = false;
    var pdfDoc = pdfExport.ExportToPdf(this.dataGrid, option);
    pdfDoc.Save(stream);
    pdfDoc.Close(true);
    SaveService saveService = new();
    saveService.SaveAndView("Export Feature.pdf", "application/pdf", stream);
}

Refer to the following image.

Exporting .NET MAUI DataGrid without headers to a PDF

Customizing the border in exported PDF

By default, the exported PDF document includes both horizontal and vertical borders. You have the option to customize the borders to be either vertical, horizontal or none.

Refer to the following code example. Here, we’ll render the PDF with horizontal borders.

private void ExportToPdf_Clicked(object sender, EventArgs e)
{
    MemoryStream stream = new MemoryStream();
    DataGridPdfExportingController pdfExport = new DataGridPdfExportingController();
    DataGridPdfExportingOption option = new DataGridPdfExportingOption();
    option.GridLineType = GridLineType.Horizontal;
    var pdfDoc = pdfExport.ExportToPdf(this.dataGrid, option);
    pdfDoc.Save(stream);
    pdfDoc.Close(true);
    SaveService saveService = new();
    saveService.SaveAndView("Export Feature.pdf", "application/pdf", stream);
}

Refer to the following image.

Customizing the border while exporting .NET MAUI DataGrid to a PDF

Applying styles for the exported PDF

You can export data to a PDF with the DefaultStyle applied to the .NET MAUI DataGrid control by setting the DataGridPdfExportingOption.CanApplyGridStyle property to true.

By default, the data will be exported without the DefaultStyle.

Refer to the following code examples.

XAML

<syncfusion:SfDataGrid x:Name="dataGrid"
                       Margin="20"
                       VerticalOptions="FillAndExpand"
                       ItemsSource="{Binding OrderInfoCollection}"
                       GridLinesVisibility="Both"
                       HeaderGridLinesVisibility="Both"
                       AutoGenerateColumnsMode="None"
                       SelectionMode="Multiple"
                       ColumnWidthMode="Auto">
 <syncfusion:SfDataGrid.DefaultStyle>
  <syncfusion:DataGridStyle RowBackground="LightBlue" HeaderRowBackground="LightGoldenrodYellow"/>
 </syncfusion:SfDataGrid.DefaultStyle>
</syncfusion:SfDataGrid>

C#

private void ExportToPdf_Clicked(object sender, EventArgs e)
{
    MemoryStream stream = new MemoryStream();
    DataGridPdfExportingController pdfExport = new DataGridPdfExportingController();
    DataGridPdfExportingOption option = new DataGridPdfExportingOption();
    option.CanApplyGridStyle = true;
    var pdfDoc = pdfExport.ExportToPdf(this.dataGrid, option);
    pdfDoc.Save(stream);
    pdfDoc.Close(true);
    SaveService saveService = new();
    saveService.SaveAndView("Export Feature.pdf", "application/pdf", stream);
}

Refer to the following image.

Applying styles to the exported grid data in a PDF

References

For more details, refer to the Export to PDF options in .NET MAUI DataGrid documentation and GitHub demo.

Supercharge your cross-platform apps with Syncfusion's robust .NET MAUI controls.

Conclusion

Thanks for reading! This blog offers an extensive guide on efficiently exporting Syncfusion.NET MAUI DataGrid to PDF, equipping developers with essential tools for a smooth process. We believe that the steps outlined here have been valuable and enlightening.

If you’re interested in delving deeper into .NET MAUI, you can easily access and evaluate it by downloading Essential Studio for .NET MAUI for free. Our esteemed customers can acquire the latest version of Essential Studio from the License and Downloads page.

Should you need any assistance or have further inquiries, contact us via our support forum, support portal, or feedback portal. We are committed to providing help and support in every possible way.

Related blogs

Meet the Author

Farjana Parveen

Farjana Parveen is a product manager at Syncfusion who oversees the development of the DataGrid, TreeGrid, and TreeView WPF and also WinUI controls. She has been a .NET developer since 2014, with expertise in WinUI, WinForms, and WPF.