How to overflow the columns to next page when exceed the page width in C# and VB.NET
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. This library also offers functionality to merge, split, stamp, forms, compress, and secure PDF files.
Using this library, you can create barcode with size of a printer by resizing the page to printer size. This sample explains how to overflow the columns to next page when exceed the page width in C# and VB.NET.
Steps for overflow the columns to next page when exceed the page width programmatically using C#:
- Create a new C# console 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 Program.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Grid; using System.Drawing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Grid; Imports System.Drawing
- Include the following code snippet in main method of Program.cs file
C#
//Creates new PDF document PdfDocument doc = new PdfDocument(); //Adds new page PdfPage page = doc.Pages.Add(); //Initialize grid PdfGrid grid = new PdfGrid(); //Add columns and header grid.Columns.Add(20); grid.Headers.Add(1); //Adding grid cell style PdfGridCellStyle cellStyle = new PdfGridCellStyle(); //Create new PDF string format instance. PdfStringFormat format = new PdfStringFormat(); format.Alignment = PdfTextAlignment.Center; //Set string format to grid cell. cellStyle.StringFormat = format; //Set cell paddings. cellStyle.CellPadding = new PdfPaddings(5, 5, 5, 5); //Assign header values for (int i = 0; i < grid.Columns.Count; i++) { grid.Headers[0].Cells[i].Value = "Header" + i; grid.Headers[0].Cells[i].Style = cellStyle; grid.Columns[i].Width = 100; grid.Rows.Add(); } //Assign row values for (int j = 0; j < grid.Rows.Count; j++) { grid.Rows[j].Height = 30; for (int cell = 0; cell < grid.Columns.Count; cell++) { grid.Rows[j].Cells[cell].Value = "Value" + j; grid.Rows[j].Cells[cell].Style= cellStyle; } } //Set true inorder to achieve overflow to the grid columns if it exceeds page width grid.Style.AllowHorizontalOverflow = true; //Draws the grid grid.Draw(page, new PointF(10, 10)); //Save the document and dispose it. doc.Save("Ouput.pdf"); doc.Close(true);
VB.NET
'Creates new PDF document Dim doc As PdfDocument = New PdfDocument() 'Adds new page Dim page As PdfPage = doc.Pages.Add() 'Initialize grid Dim grid As PdfGrid = New PdfGrid() 'Add columns and header grid.Columns.Add(20) grid.Headers.Add(1) 'Adding grid cell style Dim cellStyle As PdfGridCellStyle = New PdfGridCellStyle() 'Create new PDF string format instance. Dim format As PdfStringFormat = New PdfStringFormat() format.Alignment = PdfTextAlignment.Center 'Set string format to grid cell. cellStyle.StringFormat = format 'Set cell paddings. Dim cellPaddings As PdfPaddings = New PdfPaddings(5, 5, 5, 5) cellStyle.CellPadding = cellPaddings 'Assign header values For i As Integer = 0 To grid.Columns.Count - 1 grid.Headers(0).Cells(i).Value = "Header" & i grid.Headers(0).Cells(i).Style = cellStyle grid.Columns(i).Width = 100 grid.Rows.Add() Next 'Assign row values For j As Integer = 0 To grid.Rows.Count - 1 grid.Rows(j).Height = 30 For cell As Integer = 0 To grid.Columns.Count - 1 grid.Rows(j).Cells(cell).Value = "Value" & j grid.Rows(j).Cells(cell).Style = cellStyle Next Next 'Set true inorder to achieve overflow to the grid columns if it exceeds page width grid.Style.AllowHorizontalOverflow = True grid.Draw(page, New PointF(10, 10)) 'Save the document and dispose it. doc.Save("Ouput.pdf") doc.Close(True)
A complete work sample for how to overflow the columns to next page when exceed the page width can be downloaded from Sample.zip
By executing the program, you will get the PDF document as follows.
Output PDF sample
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.
An online sample link demonstrating the use of Grid in PDF document.
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.