How to skip grid header on drawing multiple grid in PDF 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 skip grid header on drawing multiple grid in a PDF using C# and VB.NET.
Steps to skip header on drawing multiple grid in PDF programmatically:
- Create a new C# Windows Forms 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 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
- Use the following code snippet to skip grid header on drawing multiple grid in the PDF document.
C#
PdfLayoutResult layoutResult = null; int columnCount; int count = 0; private void button1_Click(object sender, EventArgs e) { //Create new PDF document PdfDocument document = new PdfDocument(); //Add a page to the document PdfPage page = document.Pages.Add(); //Create a PdfGrid PdfGrid pdfGrid = new PdfGrid(); for (int i = 0; i <= 3; i++) { count = 0; //Create a DataTable DataTable table = new DataTable(); //Add columns to table table.Columns.Add("Line No"); table.Columns.Add("Item Name"); table.Columns.Add("Item Group"); table.Columns.Add("Description"); table.Columns.Add("Qty"); table.Columns.Add("Price"); table.Columns.Add("Line Total"); table.Columns.Add("Doc Entry"); //Assign Column count columnCount = table.Columns.Count; for (int j = 0; j < 40; j++) { //Add rows to table table.Rows.Add(new string[] { j.ToString(), "Coil", "Row material", "coil", "12", "100", "1200", i.ToString() }); } //Assign data source pdfGrid.DataSource = table; pdfGrid.RepeatHeader = true; pdfGrid.BeginCellLayout += PdfGrid_BeginCellLayout; //Create a second PdfGrid PdfGrid grid = new PdfGrid(); //Create a data table for second PdfGrid DataTable grid_dataTable = new DataTable(); //Add columns to second table grid_dataTable.Columns.Add("Header"); //Add rows grid_dataTable.Rows.Add(new string[] { "Group " + (i + 1).ToString() }); //Assign data table grid.DataSource = grid_dataTable; grid.BeginCellLayout += Grid_BeginCellLayout; //Set style grid.Style.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 8, PdfFontStyle.Bold); //Draw group information if (layoutResult == null) layoutResult = grid.Draw(page, new PointF(0, 0)); else layoutResult = grid.Draw(layoutResult.Page, new PointF(0, layoutResult.Bounds.Bottom - grid.Rows[0].Height)); PointF point = new PointF(0, layoutResult.Bounds.Bottom); // Value(50 - used defined) indicate whether the header should draw into page or not if (layoutResult.Bounds.Bottom > 50) { point = new PointF(0, layoutResult.Bounds.Bottom - layoutResult.Bounds.Height); } //Draw the table if (layoutResult == null) layoutResult = pdfGrid.Draw(page, new PointF(0, 0)); else layoutResult = pdfGrid.Draw(layoutResult.Page, point); } //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 Process.Start("Table.pdf"); } private void Grid_BeginCellLayout(object sender, PdfGridBeginCellLayoutEventArgs args) { count++; PdfGrid grid = (sender as PdfGrid); if (count <= grid.Headers.Count * grid.Columns.Count) { args.Skip = true; } } private void PdfGrid_BeginCellLayout(object sender, PdfGridBeginCellLayoutEventArgs args) { if (args.RowIndex == 0 && args.CellIndex <= columnCount && layoutResult != null) { if (layoutResult.Bounds.Y > args.Bounds.Height) { args.Skip = true; } else { args.Skip = false; } } }
VB.NET
Dim layoutResult As PdfLayoutResult = Nothing Dim columnCount As Integer Dim count As Integer = 0 Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) 'Create new Pdf document Dim document As PdfDocument = New PdfDocument() 'Add a page to the document Dim page As PdfPage = document.Pages.Add() 'Create a PdfGrid Dim pdfGrid As PdfGrid = New PdfGrid() For i As Integer = 0 To 3 count = 0 'Create a DataTable Dim table As DataTable = New DataTable() 'Add columns to table table.Columns.Add("Line No") table.Columns.Add("Item Name") table.Columns.Add("Item Group") table.Columns.Add("Description") table.Columns.Add("Qty") table.Columns.Add("Price") table.Columns.Add("Line Total") table.Columns.Add("Doc Entry") 'Assign Column count columnCount = table.Columns.Count 'Add rows to Table For j As Integer = 0 To 40 - 1 table.Rows.Add(New String() {j.ToString(), "Coil", "Row material", "coil", "12", "100", "1200", i.ToString()}) Next 'Assign data source pdfGrid.DataSource = table pdfGrid.RepeatHeader = True AddHandler pdfGrid.BeginCellLayout, New PdfGridBeginCellLayoutEventHandler(AddressOf PdfGrid_BeginCellLayout) 'Create a second PdfGrid Dim grid As PdfGrid = New PdfGrid() 'Create a data table for second PdfGrid Dim grid_dataTable As DataTable = New DataTable() 'Add columns to second table grid_dataTable.Columns.Add("Header") 'Add rows grid_dataTable.Rows.Add(New String() {"Group " & (i + 1).ToString()}) 'Assign data table grid.DataSource = grid_dataTable AddHandler grid.BeginCellLayout, New PdfGridBeginCellLayoutEventHandler(AddressOf Grid_BeginCellLayout) 'Set style grid.Style.Font = New PdfStandardFont(PdfFontFamily.Helvetica, 8, PdfFontStyle.Bold) 'Draw group information If layoutResult Is Nothing Then layoutResult = grid.Draw(page, New PointF(0, 0)) Else layoutResult = grid.Draw(layoutResult.Page, New PointF(0, layoutResult.Bounds.Bottom - grid.Rows(0).Height)) End If Dim point As PointF = New PointF(0, layoutResult.Bounds.Bottom) 'Value(50 - used defined) indicate whether the header should draw into page or not If layoutResult.Bounds.Bottom > 50 Then point = New PointF(0, layoutResult.Bounds.Bottom - layoutResult.Bounds.Height) End If 'Draw the table If layoutResult Is Nothing Then layoutResult = pdfGrid.Draw(page, New PointF(0, 0)) Else layoutResult = pdfGrid.Draw(layoutResult.Page, point) End If Next '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 Process.Start("Table.pdf") End Sub Private Sub Grid_BeginCellLayout(ByVal sender As Object, ByVal args As PdfGridBeginCellLayoutEventArgs) count += 1 Dim grid As PdfGrid = (TryCast(sender, PdfGrid)) If count <= grid.Headers.Count * grid.Columns.Count Then args.Skip = True End If End Sub Private Sub PdfGrid_BeginCellLayout(ByVal sender As Object, ByVal args As PdfGridBeginCellLayoutEventArgs) If args.RowIndex = 0 AndAlso args.CellIndex <= columnCount AndAlso layoutResult IsNot Nothing Then If layoutResult.Bounds.Y > args.Bounds.Height Then args.Skip = True Else args.Skip = False End If End If End Sub
A complete working sample can be downloaded from PDFTableSample.zip.
Take a moment to peruse the documentation, where you can find 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 with code examples.
Refer here to learn more about PDF Tables in Essential PDF library.
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.