How to export multiple DataGrid to one PDF document in .NET MAUI?
You can export multiple .NET MAUI DataGrid into a single PDF document one at a time using the DataGridPdfExportingController.ExportToPdfGrid method. Draw the exported PDF sequentially into a single PDF document using the PdfGrid.Draw method.
XAML
<StackLayout>
<Button x:Name="button" Text="Export to PDF" >
<Button.Behaviors>
<behaviors:ButtonBehavior/>
</Button.Behaviors>
</Button>
<syncfusion:SfDataGrid x:Name="dataGrid1" ItemsSource="{Binding Employees}" AutoGenerateColumnsMode="None">
<syncfusion:SfDataGrid.Columns>
<syncfusion:DataGridNumericColumn MappingName="EmployeeID" HeaderText="New Employee ID "/>
<syncfusion:DataGridTextColumn MappingName="Name" HeaderText="New Employee Name "/>
<syncfusion:DataGridTextColumn MappingName="IDNumber" HeaderText="New ID Number"/>
</syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>
<syncfusion:SfDataGrid x:Name="dataGrid2" ItemsSource="{Binding Employees}" AutoGenerateColumnsMode="None">
<syncfusion:SfDataGrid.Columns>
<syncfusion:DataGridNumericColumn MappingName="EmployeeID" HeaderText="Old Employee ID"/>
<syncfusion:DataGridTextColumn MappingName="Name" HeaderText="Old Name"/>
<syncfusion:DataGridTextColumn MappingName="IDNumber" HeaderText="Old ID Number"/>
</syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>
</StackLayout>
C#
Create a PDF document using the PdfDocument and export the SfDataGrid into a PdfGrid using the extension method ExportToPdfGrid with the DataGridPdfExportingOption, which allows customization of the data exported to the PDF.
Sequentially draw the exported PdfGrid in the PdfPage using the PdfGrid.Draw method and save the created PDF document.
private void Button_Clicked(object? sender, EventArgs e)
{
var button = sender as Button;
var stackLayout = button.Parent as StackLayout;
dataGrid1 = stackLayout.FindByName<SfDataGrid>("dataGrid1");
dataGrid2 = stackLayout.FindByName<SfDataGrid>("dataGrid2");
MemoryStream stream = new MemoryStream();
DataGridPdfExportingController pdfExport = new DataGridPdfExportingController();
DataGridPdfExportingOption option = new DataGridPdfExportingOption();
option.CanFitAllColumnsInOnePage = true;
var pdfDoc = new PdfDocument();
PdfPage page = pdfDoc.Pages.Add();
var exportToPdfGrid = pdfExport.ExportToPdfGrid(this.dataGrid1, this.dataGrid1.View,option, pdfDoc);
var exportToPdfGrid1 = pdfExport.ExportToPdfGrid(this.dataGrid2, this.dataGrid2.View, option, pdfDoc);
PdfGridLayoutFormat layoutFormat = new PdfGridLayoutFormat();
layoutFormat.Layout = PdfLayoutType.Paginate;
PdfLayoutResult result = exportToPdfGrid.Draw(page, 10, 10, layoutFormat);
exportToPdfGrid1.Draw(result.Page, 10, result.Bounds.Height + 20);
pdfDoc.Save(stream);
pdfDoc.Close(true);
SaveService saveService = new();
saveService.SaveAndView("ExportFeature.pdf", "application/pdf", stream);
}
Download the complete sample from GitHub.
Conclusion
I hope you enjoyed learning how to export multiple DataGrids to a single PDF document in .NET MAUI DataGrid.
You can refer to our .NET MAUI DataGrid feature tour page to learn about its other groundbreaking feature representations. Explore our .NET MAUI DataGrid Documentation to understand how to present and manipulate data.
For current customers, check out our .NET MAUI components on the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to explore our .NET MAUI DataGrid and other .NET MAUI components.
Please let us know in the comments section if you have any queries or require clarification. You can also contact us through our support forums, Direct-Trac or feedback portal, or the feedback portal. We are always happy to assist you!