How to Add Page in WinForms PDF Table Header Using C# and VB.NET?
Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can add page number in a PDF table header using C# and VB.NET in our Winforms PDF feature tour page.
Steps to add page number in PDF table header programmatically:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Tables; using System.Drawing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics Imports Syncfusion.Pdf.Tables Imports System.Drawing
- Use the following code snippet in button_Click method to add XMP metadata to PDF.
C#
//Create a new PDF document PdfDocument document = new PdfDocument(); //Add a page PdfPage page = document.Pages.Add(); //Create a PdfLightTable PdfLightTable lightTable = new PdfLightTable(); //Set the Data source to direct lightTable.DataSourceType = PdfLightTableDataSourceType.TableDirect; //Create columns lightTable.Columns.Add(new PdfColumn("Chapter No")); lightTable.Columns.Add(new PdfColumn("Topic")); lightTable.Columns.Add(new PdfColumn("")); //Add rows PdfRowCollection rowCollection = lightTable.Rows; rowCollection.Add(new object[] { "I", "Introduction", "" }); rowCollection.Add(new object[] { "II", "Overview", "" }); rowCollection.Add(new object[] { "III", "Syntax", "" }); //Show header in the table lightTable.Style.ShowHeader = true; //Below event is raised whenever a cell is created lightTable.BeginCellLayout += new BeginCellLayoutEventHandler(LightTable_BeginCellLayout) //Set properties to paginate the table PdfLightTableLayoutFormat format = new PdfLightTableLayoutFormat(); format.Layout = PdfLayoutType.Paginate; format.Break = PdfLayoutBreakType.FitElement; lightTable.Style.RepeatHeader = true; //Draw PdfLightTable lightTable.Draw(page, new PointF(0, 0), format); //Save the document document.Save("Table.pdf"); //Close the document document.Close(true); //This will open the PDF file so, the result will be seen in default PDF viewer System.Diagnostics.Process.Start("Table.pdf"); private void LightTable_BeginCellLayout(object sender, BeginCellLayoutEventArgs args) { if (args.CellIndex == 2) { RectangleF rect = new RectangleF(args.Bounds.X, args.Bounds.Y, args.Bounds.Width, args.Bounds.Height); //Set the standard font PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 8); PdfSolidBrush brush = new PdfSolidBrush(Color.Black); //Create page number field PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush); //Create page count field PdfPageCountField count = new PdfPageCountField(font, brush); //Add the fields in composite fields PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page No {0}", pageNumber); //Draw the composite field in footer compositeField.Draw(args.Graphics, new System.Drawing.PointF(rect.X, rect.Y)); } }
VB.NET
'Create a new PDF document Dim document As PdfDocument = New PdfDocument() 'Add a page Dim page As PdfPage = document.Pages.Add() 'Create a PdfLightTable Dim lightTable As PdfLightTable = New PdfLightTable() 'Set the Data source as direct lightTable.DataSourceType = PdfLightTableDataSourceType.TableDirect 'Create columns lightTable.Columns.Add(New PdfColumn("Chapter No")) lightTable.Columns.Add(New PdfColumn("Topic")) lightTable.Columns.Add(New PdfColumn("")) 'Add rows Dim rowCollection As PdfRowCollection = lightTable.Rows rowCollection.Add(New Object() {"I", "Introduction", ""}) rowCollection.Add(New Object() {"II", "Overview", ""}) rowCollection.Add(New Object() {"III", "Syntax", ""}) 'Show header in the table lightTable.Style.ShowHeader = True 'Below event is raised whenever a cell is created AddHandler lightTable.BeginCellLayout, AddressOf LightTable_BeginCellLayout 'Set properties to paginate the table Dim format As PdfLightTableLayoutFormat = New PdfLightTableLayoutFormat() format.Layout = PdfLayoutType.Paginate format.Break = PdfLayoutBreakType.FitElement lightTable.Style.RepeatHeader = True 'Draw PdfLightTable lightTable.Draw(page, New PointF(0, 0), format) 'Save the document document.Save("Table.pdf") 'Close the document document.Close(True) 'This will open the PDF file so, the result will be seen in default PDF viewer System.Diagnostics.Process.Start("Table.pdf") Private Sub LightTable_BeginCellLayout(ByVal sender As Object, ByVal args As BeginCellLayoutEventArgs) If args.CellIndex = 2 Then Dim rect As RectangleF = New RectangleF(args.Bounds.X, args.Bounds.Y, args.Bounds.Width, args.Bounds.Height) 'Set the standard font Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 8) Dim brush As PdfSolidBrush = New PdfSolidBrush(Color.Black) 'Create page number field Dim pageNumber As PdfPageNumberField = New PdfPageNumberField(font, brush) 'Create page count field Dim count As PdfPageCountField = New PdfPageCountField(font, brush) 'Add the fields in composite fields Dim compositeField As PdfCompositeField = New PdfCompositeField(font, brush, "Page No {0}", pageNumber) 'Draw the composite field in footer compositeField.Draw(args.Graphics, New System.Drawing.PointF(rect.X, rect.Y)) End If End Sub
A complete working sample can be downloaded from PDFTableSample.zip.
By executing the program, you will get the output document as follows.
Take a moment to peruse the documentation, where you can find the options like creating a table using PdfLightTable and PdfGrid, cell and row customization in PdfLightTable and PdfGrid, built-in table styles to PdfGrid, pagination in PdfGrid and PdfLightTable and features like adding header and footer in PDF document with code examples.
Refer here to explore the rich set of Syncfusion Essential PDF features.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.
Conclusion
I hope you enjoyed learning about how to add page in WinForms PDF table header using C# and VB.NET.
You can refer to our Winforms PDF feature tour page to know about its other groundbreaking feature representations. You can also explore our 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!