How to Insert Table Without Overlapping Header/Footer in WinForms PDF?
The Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can insert a table without overlapping the header and footer in WinForms PDF document.
Steps to insert a table without overlapping the header and footer in PDF document
- Create a new Windows Forms application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from Nuget.org.
- Include the following namespaces in the Form1.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Grid; using System.Drawing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics Imports Syncfusion.Pdf.Grid Imports System.Drawing
- Add the following code in button1_Click to insert the table without overlapping the header and footer in PDF document.
C#
//Create a new PDF document PdfDocument pdfDocument = new PdfDocument(); //Add a page to the PDF document PdfPage pdfPage = pdfDocument.Pages.Add(); //Add header pdfDocument.Template.Top = AddHeader(pdfDocument, "Syncfusion Essential PDF", "Header and Footer Demo"); //Add footer pdfDocument.Template.Bottom = AddFooter(pdfDocument); //Create a PdfGrid PdfGrid pdfGrid = new PdfGrid(); //Create a data table DataTable dataTable = new DataTable(); //Add columns to the DataTable dataTable.Columns.Add("ID", typeof(int)); dataTable.Columns.Add("Name", typeof(string)); dataTable.Columns.Add("Type", typeof(string)); dataTable.Columns.Add("Date", typeof(DateTime)); //Add rows to the DataTable for (int i = 0; i < 20; i++) { dataTable.Rows.Add(57, "AAA", "ABC", DateTime.Now); dataTable.Rows.Add(130, "BBB", "BCD", DateTime.Now); dataTable.Rows.Add(92, "CCC", "CDE", DateTime.Now); } //Assign data source pdfGrid.DataSource = dataTable; //Set properties to paginate the grid PdfGridLayoutFormat layoutFormat = new PdfGridLayoutFormat(); layoutFormat.Break = PdfLayoutBreakType.FitPage; layoutFormat.Layout = PdfLayoutType.Paginate; //Apply the built-in table style pdfGrid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable4Accent1); //Draw grid to the page of PDF document pdfGrid.Draw(pdfPage, new PointF(10, 10), layoutFormat); //Save the document pdfDocument.Save("Table.pdf"); //Close the document pdfDocument.Close(true); //This will open the PDF file and the result will be seen in the default PDF Viewer Process.Start("Table.pdf");
Helper method to the add header and footer in PDF document:
C#
/// <summary> /// Add header to the PDF document /// </summary> /// <param name="doc"></param> /// <param name="title"></param> /// <param name="description"></param> /// <returns></returns> public PdfPageTemplateElement AddHeader(PdfDocument doc, string title, string description) { RectangleF rect = new RectangleF(0, 0, doc.Pages[0].GetClientSize().Width, 50); //Create a page template PdfPageTemplateElement header = new PdfPageTemplateElement(rect); PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 24); float doubleHeight = font.Height * 2; Color activeColor = Color.FromArgb(44, 71, 120); SizeF imageSize = new SizeF(110f, 35f); //Locating the logo on the right corner of the Drawing Surface PointF imageLocation = new PointF(doc.Pages[0].GetClientSize().Width - imageSize.Width - 20, 5); PdfImage img = new PdfBitmap("../../Data/logo.png"); //Draw the image in the Header. header.Graphics.DrawImage(img, imageLocation, imageSize); PdfSolidBrush brush = new PdfSolidBrush(activeColor); PdfPen pen = new PdfPen(Color.DarkBlue, 3f); font = new PdfStandardFont(PdfFontFamily.Helvetica, 16, PdfFontStyle.Bold); //Set formattings for the text PdfStringFormat format = new PdfStringFormat(); format.Alignment = PdfTextAlignment.Center; format.LineAlignment = PdfVerticalAlignment.Middle; //Draw title header.Graphics.DrawString(title, font, brush, new RectangleF(0, 0, header.Width, header.Height), format); brush = new PdfSolidBrush(Color.Gray); font = new PdfStandardFont(PdfFontFamily.Helvetica, 6, PdfFontStyle.Bold); format = new PdfStringFormat(); format.Alignment = PdfTextAlignment.Left; format.LineAlignment = PdfVerticalAlignment.Bottom; //Draw description header.Graphics.DrawString(description, font, brush, new RectangleF(0, 0, header.Width, header.Height - 8), format); //Draw some lines in the header pen = new PdfPen(Color.DarkBlue, 0.7f); header.Graphics.DrawLine(pen, 0, 0, header.Width, 0); pen = new PdfPen(Color.DarkBlue, 2f); header.Graphics.DrawLine(pen, 0, 03, header.Width + 3, 03); pen = new PdfPen(Color.DarkBlue, 2f); header.Graphics.DrawLine(pen, 0, header.Height - 3, header.Width, header.Height - 3); header.Graphics.DrawLine(pen, 0, header.Height, header.Width, header.Height); return header; } /// <summary> /// Add footer to the PDF document /// </summary> /// <param name="doc"></param> /// <returns></returns> public PdfPageTemplateElement AddFooter(PdfDocument doc) { RectangleF rect = new RectangleF(0, 0, doc.Pages[0].GetClientSize().Width, 50); //Create a page template PdfPageTemplateElement footer = new PdfPageTemplateElement(rect); PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 7, PdfFontStyle.Bold); PdfSolidBrush brush = new PdfSolidBrush(Color.Black); //Create a page number field PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush); //Create a page count field PdfPageCountField count = new PdfPageCountField(font, brush); //Add the fields in composite fields PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, count); compositeField.Bounds = footer.Bounds; //Draw the composite field in footer compositeField.Draw(footer.Graphics, new PointF(470, 40)); return footer; }
VB.NET
'Create a new PDF document Dim pdfDocument As PdfDocument = New PdfDocument() 'Add a page to the PDF document Dim pdfPage As PdfPage = pdfDocument.Pages.Add() 'Add header pdfDocument.Template.Top = AddHeader(pdfDocument, "Syncfusion Essential PDF", "Header and Footer Demo") 'Add footer pdfDocument.Template.Bottom = AddFooter(pdfDocument) 'Create a PdfGrid Dim pdfGrid As PdfGrid = New PdfGrid() 'Create a data table Dim dataTable As DataTable = New DataTable() 'Add columns to the DataTable dataTable.Columns.Add("ID", GetType(Integer)) dataTable.Columns.Add("Name", GetType(String)) dataTable.Columns.Add("Type", GetType(String)) dataTable.Columns.Add("Date", GetType(DateTime)) 'Add rows to the DataTable For i As Integer = 0 To 20 - 1 dataTable.Rows.Add(57, "AAA", "ABC", DateTime.Now) dataTable.Rows.Add(130, "BBB", "BCD", DateTime.Now) dataTable.Rows.Add(92, "CCC", "CDE", DateTime.Now) Next 'Assign data source pdfGrid.DataSource = dataTable 'Set properties to paginate the grid Dim layoutFormat As PdfGridLayoutFormat = New PdfGridLayoutFormat() layoutFormat.Break = PdfLayoutBreakType.FitPage layoutFormat.Layout = PdfLayoutType.Paginate 'Apply the built-in table style pdfGrid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable4Accent1) 'Draw grid to the page of PDF document pdfGrid.Draw(pdfPage, New PointF(10, 10), layoutFormat) 'Save the document pdfDocument.Save("Table.pdf") 'Close the document pdfDocument.Close(True) 'This will open the PDF file and the result will be seen in default PDF Viewer Process.Start("Table.pdf")
Helper method to add the header and footer in PDF document:
VB.NET
''' <summary> ''' Add header to PDF document ''' </summary> ''' <param name="doc"></param> ''' <param name="title"></param> ''' <param name="description"></param> ''' <returns></returns> Public Function AddHeader(ByVal doc As PdfDocument, ByVal title As String, ByVal description As String) As PdfPageTemplateElement Dim rect As RectangleF = New RectangleF(0, 0, doc.Pages(0).GetClientSize().Width, 50) 'Create a page template Dim header As PdfPageTemplateElement = New PdfPageTemplateElement(rect) Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 24) Dim doubleHeight As Single = font.Height * 2 Dim activeColor As Color = Color.FromArgb(44, 71, 120) Dim imageSize As SizeF = New SizeF(110.0F, 35.0F) 'Locating the logo on the right corner of the Drawing Surface Dim imageLocation As PointF = New PointF(doc.Pages(0).GetClientSize().Width - imageSize.Width - 20, 5) Dim img As PdfImage = New PdfBitmap("../../Data/logo.png") 'Draw the image in the Header header.Graphics.DrawImage(img, imageLocation, imageSize) Dim brush As PdfSolidBrush = New PdfSolidBrush(activeColor) Dim pen As PdfPen = New PdfPen(Color.DarkBlue, 3.0F) font = New PdfStandardFont(PdfFontFamily.Helvetica, 16, PdfFontStyle.Bold) 'Set formattings for the text Dim format As PdfStringFormat = New PdfStringFormat() format.Alignment = PdfTextAlignment.Center format.LineAlignment = PdfVerticalAlignment.Middle 'Draw title header.Graphics.DrawString(title, font, brush, New RectangleF(0, 0, header.Width, header.Height), format) brush = New PdfSolidBrush(Color.Gray) font = New PdfStandardFont(PdfFontFamily.Helvetica, 6, PdfFontStyle.Bold) format = New PdfStringFormat() format.Alignment = PdfTextAlignment.Left format.LineAlignment = PdfVerticalAlignment.Bottom 'Draw description header.Graphics.DrawString(description, font, brush, New RectangleF(0, 0, header.Width, header.Height - 8), format) 'Draw some lines in the header pen = New PdfPen(Color.DarkBlue, 0.7F) header.Graphics.DrawLine(pen, 0, 0, header.Width, 0) pen = New PdfPen(Color.DarkBlue, 2.0F) header.Graphics.DrawLine(pen, 0, 3, header.Width + 3, 3) pen = New PdfPen(Color.DarkBlue, 2.0F) header.Graphics.DrawLine(pen, 0, header.Height - 3, header.Width, header.Height - 3) header.Graphics.DrawLine(pen, 0, header.Height, header.Width, header.Height) Return header End Function ''' <summary> ''' Add footer to PDF document ''' </summary> ''' <param name="doc"></param> ''' <returns></returns> Public Function AddFooter(ByVal doc As PdfDocument) As PdfPageTemplateElement Dim rect As RectangleF = New RectangleF(0, 0, doc.Pages(0).GetClientSize().Width, 50) 'Create a page template Dim footer As PdfPageTemplateElement = New PdfPageTemplateElement(rect) Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 7, PdfFontStyle.Bold) Dim brush As PdfSolidBrush = New PdfSolidBrush(Color.Black) 'Create a page number field Dim pageNumber As PdfPageNumberField = New PdfPageNumberField(font, brush) 'Create a page count field 'Add the fields in composite fields Dim count As PdfPageCountField = New PdfPageCountField(font, brush) Dim compositeField As PdfCompositeField = New PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, count) compositeField.Bounds = footer.Bounds 'Draw the composite field in footer compositeField.Draw(footer.Graphics, New PointF(470, 40)) Return footer End Function
A complete working sample can be download from InsertTableSample.zip.
By executing the program, you will get the output document as follows,
Take a moment to peruse the documentation. You can find more details about cell customization, rows, and column customization, table pagination, and build in table styles. Also, features like add Header and Footer in PDF document.
Refer to here to explore a rich set of Syncfusion Essential® PDF features.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or the NuGet feed, include a license key in your product. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without trail message.
See Also:
Create a table in PDF document using PdfGrid
Create a table in PDF document using PdfLightTable
Add the header and footer in PDF document
I hope you enjoyed learning about how to insert table without overlapping header/footer in WinForms PDF.
You can refer to our WinForms PDF featuretour page to know about its other groundbreaking feature representations and 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!