Category / Section
How to paginate a PDF grid across multiple pages in C#
3 mins read
The Syncfusion Essential® PDF is a feature-rich and high performance .NET PDF library used to create, read, and edit PDF documents programmatically without Adobe dependencies. Using this library, you can paginate PDF grid across multiple pages using C#.
Steps to add pagination in pdf grid programmatically:
- Create a new console application project.
- Install the Syncfusion.Pdf.Net.Core NuGet package as a reference to your console application from Nuget.org.
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Grid;
using Syncfusion.Pdf;
using Syncfusion.Drawing;
- Use the following code sample in Program.cs to add pagination in pdf grid.
C#
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a new page to the document
PdfPage page = document.Pages.Add();
// Create and draw PDF grid
AddGrid(page);
// Create a memory stream to save the document
MemoryStream stream = new MemoryStream();
// Save the PDF document to the memory stream
document.Save(stream);
// Close the document and release resources
document.Close(true);
// Write the PDF file to disk
File.WriteAllBytes(@"OutputPdf.pdf", stream.ToArray());
// Method to add content to the PDF page
void AddGrid(PdfPage page)
{
// Create a new PDF grid
PdfGrid grid = new PdfGrid();
// Add 2 columns to the grid
grid.Columns.Add(2);
// Add a header row to the grid
grid.Headers.Add(1);
// Access the header row
PdfGridRow pdfGridHeader = grid.Headers[0];
pdfGridHeader.Cells[0].Value = "Tittle";
pdfGridHeader.Cells[1].Value = "Description";
// Loop to add multiple rows to the grid
for (int j = 0; j < 7; j++)
{
// Add a new row to the grid
PdfGridRow pdfGridRow = grid.Rows.Add();
// Set values for the row cells
pdfGridRow.Cells[0].Value = "Software";
pdfGridRow.Cells[1].Value = "Building on previous innovations in mathematics and technology, software was created for the programmable digital computers that emerged in the late 1940s and was necessary to realize their usefulness. The first software was tied closely to the underlying computer hardware, but over time, the lower layers of the system have become more standardized, and software has become increasingly portable between different systems and abstracted from the underlying machine code. Operating systems manage the hardware resources and mediate between different applications that accomplish tasks for the user. Programming languages are the format in which software is written, and must be both human-readable and capable of being translated into unambiguous instructions for computer hardware. Compilers or interpreters are needed to link a program with other code that it relies on and convert the software into machine code that can be executed on the hardware.";
}
PdfGridLayoutFormat layoutFormat = new PdfGridLayoutFormat
{
// Fit grid to the page, break if needed
Break = PdfLayoutBreakType.FitPage,
// Paginate the grid if it spans multiple pages
Layout = PdfLayoutType.Paginate
};
// Draw the grid on the page at specified position
grid.Draw(page, new PointF(0, 200), layoutFormat);
}
A complete working sample can be downloaded from Add_Paginate_in_PDFGrid.zip
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation for using Grid in PDF document, where you will find other options like support for cell customization, rows and columns customization, and built in table styles, pagination.
Refer here to explore the rich set of Syncfusion Essential® PDF features.