Exporting DataGrid to PDF Made Easy in .NET MAUI
Tags: dotnetmaui, datagrid, mobile, maui
Published: 2024-05-08 14:25:13

---
title: Exporting DataGrid to PDF Made Easy in .NET MAUI
published: true
date: 2024-05-06 11:51:43 UTC
tags: dotnetmaui, datagrid, mobile, maui
canonical_url: https://www.syncfusion.com/blogs/post/export-dotnet-maui-datagrid-to-pdf
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u4q04rcz93w669l0npzj.png
---
**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](https://www.syncfusion.com/maui-controls/maui-datagrid ".NET MAUI DataGrid control") 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](https://help.syncfusion.com/maui/datagrid/getting-started "Getting started with .NET MAUI DataGrid").

## Export .NET MAUI DataGrid to PDF

The [Syncfusion.Maui.DataGridExport](https://www.nuget.org/packages/Syncfusion.Maui.DataGridExport "Syncfusion.Maui.DataGridExport NuGet package") 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.

```xml
<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](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.DataGrid.Exporting.DataGridPdfExportingController.html "DataGridPdfExportingController Class for .NET MAUI DataGrid") class provides all PDF exporting methods. Using the [ExportToPdf](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.DataGrid.Exporting.DataGridPdfExportingController.html#Syncfusion_Maui_DataGrid_Exporting_DataGridPdfExportingController_ExportToPdf_Syncfusion_Maui_DataGrid_SfDataGrid_ "ExportToPdf(SfDataGrid) method for .NET MAUI DataGrid") or [ExportToPdfGrid](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.DataGrid.Exporting.DataGridPdfExportingController.html#Syncfusion_Maui_DataGrid_Exporting_DataGridPdfExportingController_ExportToPdfGrid_Syncfusion_Maui_DataGrid_SfDataGrid_Syncfusion_Maui_Data_ICollectionViewAdv_Syncfusion_Maui_DataGrid_Exporting_DataGridPdfExportingOption_Syncfusion_Pdf_PdfDocument_ "ExportToPdfGrid method for .NET MAUI DataGrid") method, you can easily export the DataGrid content to a PDF document.

Refer to the following code example.

```csharp
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](https://help.syncfusion.com/maui/datagrid/export-to-pdf#save-service-class-in-portable-project "Save Service class in portable project") class to save the PDF document at a specific location and view it on your machine.

Refer to the following image.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2024/05/Exporting-.NET-MAUI-DataGrid-data-to-PDF..png" alt="Exporting .NET MAUI DataGrid data to PDF" style="width:100%">

  <figcaption>Exporting .NET MAUI DataGrid data to PDF</figcaption>

</figure>

## PDF exporting with customization

You can also customize the exported PDF document using the [DataGridPdfExportingOption](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.DataGrid.Exporting.DataGridPdfExportingOption.html "DataGridPdfExportingOption class for .NET MAUI DataGrid").

### 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](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.DataGrid.Exporting.DataGridPdfExportingOption.html#Syncfusion_Maui_DataGrid_Exporting_DataGridPdfExportingOption_ExcludeColumns "ExcludeColumns property for .NET MAUI DataGrid") list.

Refer to the following code example.

```csharp
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.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2024/05/Excluding-specific-columns-in-.NET-MAUI-DataGrid-while-exporting-to-a-PDF-1.png" alt="Excluding specific columns in .NET MAUI DataGrid while exporting to a PDF" style="width:100%">

  <figcaption>Excluding specific columns in .NET MAUI DataGrid while exporting to a PDF</figcaption>

</figure>

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

```csharp
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.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2024/05/Exporting-all-columns-in-the-.NET-MAUI-DataGrid-to-a-single-page-in-a-PDF-1.png" alt="Exporting all columns in the .NET MAUI DataGrid to a single page in a PDF" style="width:100%">

  <figcaption>Exporting all columns in the .NET MAUI DataGrid to a single page in a PDF</figcaption>

</figure>

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

```csharp
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.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2024/05/Exporting-.NET-MAUI-DataGrid-without-headers-to-a-PDF.png" alt="Exporting .NET MAUI DataGrid without headers to a PDF" style="width:100%">

  <figcaption>Exporting .NET MAUI DataGrid without headers to a PDF</figcaption>

</figure>

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

```csharp
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.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2024/05/Customizing-the-border-while-exporting-.NET-MAUI-DataGrid-to-a-PDF-1.png" alt="Customizing the border while exporting .NET MAUI DataGrid to a PDF" style="width:100%">

  <figcaption>Customizing the border while exporting .NET MAUI DataGrid to a PDF</figcaption>

</figure>

### 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](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.DataGrid.Exporting.DataGridPdfExportingOption.html#Syncfusion_Maui_DataGrid_Exporting_DataGridPdfExportingOption_CanApplyGridStyle "CanApplyGridStyle property for .NETMAUI DataGrid") property to **true**.

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

Refer to the following code examples.

**XAML**

```xml
<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#**

```csharp
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.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2024/05/Applying-styles-to-the-exported-grid-data-in-a-PDF.png" alt="Applying styles to the exported grid data in a PDF" style="width:100%">

  <figcaption>Applying styles to the exported grid data in a PDF</figcaption>

</figure>

## References

For more details, refer to the [Export to PDF options in .NET MAUI DataGrid documentation](https://help.syncfusion.com/maui/datagrid/export-to-pdf#exporting-options "Export to PDF options in .NET MAUI DataGrid documentation") and [GitHub demo](https://github.com/SyncfusionExamples/Exporting-DataGrid-to-PDF-Made-Easy-in-.NET-MAUI "Export .NET MAUI DataGrid to PDF GitHub demo").

## Conclusion

Thanks for reading! This blog offers an extensive guide on efficiently exporting Syncfusion[.NET MAUI DataGrid](https://www.syncfusion.com/maui-controls/maui-datagrid ".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](https://www.syncfusion.com/downloads/maui "Get free evaluation of the Essential Studio for .NET MAUI") for free. Our esteemed customers can acquire the latest version of Essential Studio from the [License and Downloads](https://www.syncfusion.com/account/downloads "Essential Studio License and Downloads page")page.

Should you need any assistance or have further inquiries, contact us via our [support forum](https://www.syncfusion.com/forums "Syncfusion Support Forum"), [support portal](https://support.syncfusion.com/ "Syncfusion Support Portal"), or [feedback portal](https://www.syncfusion.com/feedback/ "Syncfusion Feedback Portal"). We are committed to providing help and support in every possible way.

## Related blogs

- [Exporting DataGrid to Excel Made Easy in .NET MAUI](https://www.syncfusion.com/blogs/post/export-dotnet-maui-datagrid-to-excel "Blog: Exporting DataGrid to Excel Made Easy in .NET MAUI")
- [Easily Create a Directional Compass Using .NET MAUI Radial Gauge](https://www.syncfusion.com/blogs/post/directional-compass-maui-radial-gauge "Blog: Easily Create a Directional Compass Using .NET MAUI Radial Gauge")
- [Creating a .NET MAUI Sunburst Chart to Visualize the Most Popular Wikipedia Articles](https://www.syncfusion.com/blogs/post/dotnet-maui-sunburst-chart-wikipedia "Blog: Chart of the Week: Creating a .NET MAUI Sunburst Chart to Visualize the Most Popular Wikipedia Articles")
- [Create a Modern Conversational UI with the .NET MAUI Chat Control](https://www.syncfusion.com/blogs/post/conversational-ui-dotnet-maui-chat "Blog: Create a Modern Conversational UI with the .NET MAUI Chat Control")