How to add page numbers to the exported pdf in WinForms DataGrid?
Adding page number to exported pdf document
You can add page numbers in the header or footer of the exported PDF document in WinForms Grid using the dynamic fields: PdfPageNumberField and PdfCompositeField.
C#
PdfExportingOptions options = new PdfExportingOptions(); options.RepeatHeaders = true; options.ExportGroupSummary = true; options.ExportFormat = true; options.FitAllColumnsInOnePage = false; options.ExportTableSummary = true; //Set document information. Syncfusion.Pdf.PdfDocument document = new Syncfusion.Pdf.PdfDocument(); RectangleF bounds = new RectangleF(0, 10, document.Pages[0].GetClientSize().Width, 50); PdfPageTemplateElement footer = new PdfPageTemplateElement(bounds); PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 7); PdfBrush brush = new PdfSolidBrush(Color.Black); //Create page number field. PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush); //Add the fields in composite fields. PdfCompositeField compositeField = new PdfCompositeField(font, brush, "{0}", pageNumber); compositeField.Bounds = footer.Bounds; //Draw the composite field in footer. compositeField.Draw(footer.Graphics, new PointF(250, 40)); //Add the footer template at the bottom. document.Template.Bottom = footer; var pdfGrid = sfDataGrid1.ExportToPdfGrid(sfDataGrid1.View, options); pdfGrid.Draw(page, new PointF());
VB
Dim options As New PdfExportingOptions() options.RepeatHeaders = True options.ExportGroupSummary = True options.ExportFormat = True options.FitAllColumnsInOnePage = False options.ExportTableSummary = True 'Set document information. Dim document As New Syncfusion.Pdf.PdfDocument() Dim page = document.Pages.Add() Dim bounds As New RectangleF(0, 10, document.Pages(0).GetClientSize().Width, 50) Dim footer As New PdfPageTemplateElement(bounds) Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 7) Dim brush As PdfBrush = New PdfSolidBrush(Color.Black) 'Create page number field. Dim pageNumber As New PdfPageNumberField(font, brush) 'Add the fields in composite fields. Dim compositeField As New PdfCompositeField(font, brush, "{0}", pageNumber) compositeField.Bounds = footer.Bounds 'Draw the composite field in footer. compositeField.Draw(footer.Graphics, New PointF(250, 40)) 'Add the footer template at the bottom. document.Template.Bottom = footer Dim pdfGrid = sfDataGrid1.ExportToPdfGrid(sfDataGrid1.View, options) pdfGrid.Draw(page, New PointF())
Samples:
C# :
PageNumber
VB :
PageNumber
Reference link: https://help.syncfusion.com/windowsforms/datagrid/exporttopdf
I hope you enjoyed learning about how to add page numbers to the exported pdf in WinForms DataGrid.
You can refer to our WinForms Grid Control’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms Grid Control documentation to understand how to present and manipulate data.
For current customers, you can check out our WinForms components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinForms Grid Control and other WinForms components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!