Exporting DataGrid to PDF Made Easy in .NET MAUI
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (178).NET Core  (29).NET MAUI  (214)Angular  (110)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (223)BoldSign  (15)DocIO  (24)Essential JS 2  (109)Essential Studio  (200)File Formats  (68)Flutter  (133)JavaScript  (225)Microsoft  (120)PDF  (81)Python  (1)React  (105)Streamlit  (1)Succinctly series  (131)Syncfusion  (936)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (52)Windows Forms  (61)WinUI  (71)WPF  (162)Xamarin  (161)XlsIO  (38)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (9)Business intelligence  (55)Button  (4)C#  (156)Chart  (138)Chart of the week  (53)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (67)Development  (650)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (6)Essential Tools  (14)Excel  (43)Extensions  (22)File Manager  (7)Gantt  (19)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (512)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (54)Security  (4)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (12)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (393)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (607)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Exporting DataGrid to PDF Made Easy in .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
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
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
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
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
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
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.

Test Flight
App Center Badge
Google Play Store Badge
Microsoft Badge
Github Store Badge

Related blogs

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed
Syncfusion Ad