How to add page numbers to the exported pdf in WinForms DataGrid?
You can add page numbers in the header
or footer of the exported PDF document in WinForms DataGrid (SfDataGrid) 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
Take a moment to peruse the WinForms DataGrid - ExportToPdf documentation, where you can find about the ExportToPdf with code examples.
Conclusion
I hope you enjoyed learning about how to show row validation without hovering on the error icon in DataGrid.
You can refer to our WinForms DataGrid feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms DataGrid documentation to understand how to create and manipulate data.
For current customers, you can check out our 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 other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!