How to create Header and Footer in PDF document in .NET MAUI DataGrid (SfDataGrid)?
The .NET MAUI DataGrid supports exporting data to PDF, complete with headers and footers. To achieve this, utilize the ExportToPdf method by passing the SfDataGrid as an argument. The header and footer can be crafted in the HeaderAndFooterExporting event using the PdfDocumentTemplate property from the event argument.
The following code demonstrates how to subscribe to the HeaderAndFooterExporting event and create headers and footers for the exported PDF document.
XAML:
<Grid RowDefinitions="60,*"
ColumnDefinitions="*,*">
<Button Text="ExportToPdf"
Grid.Row="0"
Margin="5"
Grid.Column="0"
Clicked="Button_Clicked" />
<syncfusion:SfDataGrid x:Name="dataGrid"
Grid.Column="0"
Grid.ColumnSpan="2"
Grid.Row="1"
ColumnWidthMode="Auto"
ItemsSource="{Binding Employees}">
</syncfusion:SfDataGrid>
</Grid>
xaml.cs:
private void Button_Clicked(object sender, EventArgs e)
{
MemoryStream stream = new MemoryStream();
DataGridPdfExportingController pdfExport = new DataGridPdfExportingController();
DataGridPdfExportingOption option = new DataGridPdfExportingOption();
pdfExport.HeaderAndFooterExporting += PdfExport_HeaderAndFooterExporting;
var pdfDoc = pdfExport.ExportToPdf(this.dataGrid, option);
pdfDoc.Save(stream);
pdfDoc.Close(true);
SaveService saveService = new();
saveService.SaveAndView("ExportFeature.pdf", "application/pdf", stream);
}
private void PdfExport_HeaderAndFooterExporting(object? sender, DataGridPdfHeaderFooterEventArgs e)
{
float Width = e.PdfPage.GetClientSize().Width;
PdfPageTemplateElement header = new PdfPageTemplateElement(Width, 100);
PdfPageTemplateElement footer = new PdfPageTemplateElement(Width, 100);
PdfFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 16);
header.Graphics.DrawString("Header", font, PdfBrushes.Black, new Syncfusion.Drawing.PointF((Width / 2) - 20, 0));
e.PdfDocumentTemplate.Top = header;
footer.Graphics.DrawString("Footer", font, PdfBrushes.Black, new Syncfusion.Drawing.PointF((Width / 2) - 20, 20));
e.PdfDocumentTemplate.Bottom = footer;
}
Download the complete sample from GitHub.
Conclusion
I hope you enjoyed learning about how to create a Header and Footer in a PDF document in .NET MAUI DataGrid (SfDataGrid).
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!