Repeat the header rows in PDF table in C#/VB.NET

2021-07-07 07:11:20 Written by  support iceblue
Rate this item
(0 votes)

This article will demonstrate how to repeat the table’s header row in C#/VB.NET by using Spire.PDF for .NET.

C#
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Grid;
using System.Drawing;

namespace PDFGrid
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a pdf document
            PdfDocument doc = new PdfDocument();

            //Add a page to pdf
            PdfPageBase page = doc.Pages.Add();

            //Create a PdfGrid object
            PdfGrid grid = new PdfGrid();

            //Set the cell padding of the grid
            grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);

            //Set the Columns of the grid
            grid.Columns.Add(3);

            //Set the header rows and define the data
            PdfGridRow[] pdfGridRows = grid.Headers.Add(2);
            for (int i = 0; i < pdfGridRows.Length; i++)
            {
                pdfGridRows[i].Style.Font = new PdfTrueTypeFont(new Font("Arial", 11f, FontStyle.Regular), true);
                pdfGridRows[i].Cells[0].Value = "Vendor Name";
                pdfGridRows[i].Cells[1].Value = "Address";
                pdfGridRows[i].Cells[2].Value = "City";
            }

            //Repeat the table header rows if the grid exceed one page
            grid.RepeatHeader = true;


            for (int i = 0; i < 60; i++)
            {
                PdfGridRow row = grid.Rows.Add();

                //Add the data to the table
                for (int j = 0; j < grid.Columns.Count; j++)
                {
                    row.Cells[j].Value = "(Row " + i + ", column " + j + ")";
                }
            }

            //draw grid on the pdf page
            PdfLayoutResult pdfLayoutResult = grid.Draw(page, new PointF(0, 20));
            float y = pdfLayoutResult.Bounds.Y + pdfLayoutResult.Bounds.Height;
            PdfPageBase currentPage = pdfLayoutResult.Page;


            //Save the doucment to file
            doc.SaveToFile("PDFGrid.pdf");

        }
    }
}
VB.NET
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Grid
Imports System.Drawing

Namespace PDFGrid
    
    Class Program
        
        Private Shared Sub Main(ByVal args() As String)
            'Create a pdf document
            Dim doc As PdfDocument = New PdfDocument
            'Add a page to pdf
            Dim page As PdfPageBase = doc.Pages.Add
            'Create a PdfGrid object
            Dim grid As PdfGrid = New PdfGrid
            'Set the cell padding of the grid
            grid.Style.CellPadding = New PdfPaddings(1, 1, 1, 1)
            'Set the Columns of the grid
            grid.Columns.Add(3)
            'Set the header rows and define the data
            Dim pdfGridRows() As PdfGridRow = grid.Headers.Add(2)
            Dim i As Integer = 0
            Do While (i < pdfGridRows.Length)
                pdfGridRows(i).Style.Font = New PdfTrueTypeFont(New Font("Arial", 11!, FontStyle.Regular), true)
                pdfGridRows(i).Cells(0).Value = "Vendor Name"
                pdfGridRows(i).Cells(1).Value = "Address"
                pdfGridRows(i).Cells(2).Value = "City"
                i = (i + 1)
            Loop
            
            'Repeat the table header rows if the grid exceed one page
            grid.RepeatHeader = true
            Dim i As Integer = 0
            Do While (i < 60)
                Dim row As PdfGridRow = grid.Rows.Add
                'Add the data to the table
                Dim j As Integer = 0
                Do While (j < grid.Columns.Count)
                    row.Cells(j).Value = ("(Row "  _
                                + (i + (", column "  _
                                + (j + ")"))))
                    j = (j + 1)
                Loop
                
                i = (i + 1)
            Loop
            
            'draw grid on the pdf page
            Dim pdfLayoutResult As PdfLayoutResult = grid.Draw(page, New PointF(0, 20))
            Dim y As Single = (pdfLayoutResult.Bounds.Y + pdfLayoutResult.Bounds.Height)
            Dim currentPage As PdfPageBase = pdfLayoutResult.Page
            'Save the doucment to file
            doc.SaveToFile("PDFGrid.pdf")
        End Sub
    End Class
End Namespace

Effective screenshot of repeating the table's header row:

Repeat the header rows in PDF table in C#, VB.NET

Additional Info

  • tutorial_title:
Last modified on Sunday, 26 September 2021 02:03