Category / Section
How to create Header and Footer in PDF document?
1 min read
SfDataGrid provides support for exporting the data to PDF with header and footer. You can export the data to PDF by using the ExportToPdf method by passing the SfDataGrid as an argument. You can create the header and footer in the HeaderAndFooterExporting event using the PdfDocumentTemplate property from the event arugment.
The below code illustrates how to hook the HeaderAndFooterExporting event and create header and footer for the exported PDF document.
private void ExportToPDF(object sender,EventArgs e) { DataGridPdfExportingController pdfExport = new DataGridPdfExportingController(); pdfExport.HeaderAndFooterExporting += PdfExport_HeaderAndFooterExporting; MemoryStream stream = new MemoryStream(); var file = pdfExport.ExportToPdf(this.datagrid); file.Save(stream); file.Close(true); Xamarin.Forms.DependencyService.Get<ISave>().Save("Datagrid.pdf", "application/pdf", stream); } private void PdfExport_HeaderAndFooterExporting(object sender, PdfHeaderFooterEventArgs 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 PointF((Width / 2) - 20, 0)); e.PdfDocumentTemplate.Top = header; footer.Graphics.DrawString("Footer", font, PdfBrushes.Black, new PointF((Width / 2) - 20, 20)); e.PdfDocumentTemplate.Bottom = footer; }
Screenshot:
Sample Link: