)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to draw grid in a PDF using C# and VB.NET

Platform: WinForms |
Control: PDF |
Published Date: October 17, 2018 |
Last Revised Date: May 2, 2019
Tags: grid, pdf, pdfgrid

Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit the PDF documents. Using this library, you can create the PDF and draw grid with data sources from data set, data table, arrays, and IEnumerable objects using PdfLightTable class in C# and VB.NET.

Steps to draw Grid on PDF programmatically:

  1. Create a new C# console application project. creation of console application project
  2. Install the Syncfusion.Pdf.WinForms NuGet packages as reference to your .NET Framework application from NuGet.org. NuGet package reference
  3. Include the following namespaces in the Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Grid;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports Syncfusion.Pdf.Grid

 

  1. Use the following code snippet to create the PDF and draw table.

C#

//Create a new PDF document
PdfDocument document = new PdfDocument();
//Add a page
PdfPage page = document.Pages.Add();
//Create a PdfGrid
PdfGrid pdfGrid = new PdfGrid();
//Create a DataTable
DataTable dataTable = new DataTable();
//Add columns to the DataTable
dataTable.Columns.Add("ID");
dataTable.Columns.Add("Name");
dataTable.Columns.Add("Age");
dataTable.Columns.Add("Blood group");
//Add rows to the DataTable
for (int i = 0; i < 12; i++)
{
    dataTable.Rows.Add(new object[] { "E01", "Clay", "45", "B Positive" });
    dataTable.Rows.Add(new object[] { "E02", "Thomas", "25", "O Positive" });
    dataTable.Rows.Add(new object[] { "E03", "George", "32", "AB Positive" });
    dataTable.Rows.Add(new object[] { "E04", "Stefan", "19", "O Negative" });
    dataTable.Rows.Add(new object[] { "E05", "Mathew", "73", "B Negative" });
}
//Assign data source
pdfGrid.DataSource = dataTable;
//Create string format for PdfGrid
PdfStringFormat format = new PdfStringFormat();
format.Alignment = PdfTextAlignment.Center;
format.LineAlignment = PdfVerticalAlignment.Bottom;
//Assign string format for each column in PdfGrid
foreach (PdfGridColumn column in pdfGrid.Columns)
    column.Format = format;
//Apply a built-in style
pdfGrid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable4Accent6);
//Set properties to paginate the grid
PdfGridLayoutFormat layoutFormat = new PdfGridLayoutFormat();
layoutFormat.Break = PdfLayoutBreakType.FitPage;
layoutFormat.Layout = PdfLayoutType.Paginate;
//Draw grid to the page of PDF document
pdfGrid.Draw(page, new PointF(10, 10), layoutFormat);
//Save the document
document.Save("PdfGrid.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("PdfGrid.pdf");

 

VB.NET

'Create a new PDF document
Dim document As PdfDocument = New PdfDocument
'Add a page
Dim page As PdfPage = document.Pages.Add
'Create a PdfGrid
Dim pdfGrid As PdfGrid = New PdfGrid
Dim dataTable As DataTable = New DataTable
'Add columns to the DataTable
dataTable.Columns.Add("ID")
dataTable.Columns.Add("Name")
dataTable.Columns.Add("Age")
dataTable.Columns.Add("Blood group")
'Add rows to the DataTable
Dim i As Integer = 0
Do While (i < 12)
    dataTable.Rows.Add(New Object() {"E01", "Clay", "45", "B Positive"})
    dataTable.Rows.Add(New Object() {"E02", "Thomas", "25", "O Positive"})
    dataTable.Rows.Add(New Object() {"E03", "George", "32", "AB Positive"})
    dataTable.Rows.Add(New Object() {"E04", "Stefan", "19", "O Negative"})
    dataTable.Rows.Add(New Object() {"E05", "Mathew", "73", "B Negative"})
    i = (i + 1)
Loop
'Assign data source
pdfGrid.DataSource = dataTable
‘Create string format for PdfGrid
Dim format As PdfStringFormat = New PdfStringFormat
format.Alignment = PdfTextAlignment.Center
format.LineAlignment = PdfVerticalAlignment.Bottom
'Assign string format for each column in PdfGrid
For Each column As PdfGridColumn In pdfGrid.Columns
    column.Format = format
Next
'Apply a built-in style
pdfGrid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable4Accent6)
Dim layoutFormat As PdfGridLayoutFormat = New PdfGridLayoutFormat
layoutFormat.Break = PdfLayoutBreakType.FitPage
layoutFormat.Layout = PdfLayoutType.Paginate
'Draw grid to the page of PDF document
pdfGrid.Draw(page, New PointF(10, 10), layoutFormat)
'Save the document
document.Save("PdfGrid.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("PdfGrid.pdf")

 

A complete working sample can be downloaded from PdfGridSample.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.

Note:

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.

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon