)
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 create table in a PDF file from DataTable?

Platform: WinForms |
Control: PDF |
Published Date: August 13, 2018 |
Last Revised Date: May 3, 2019

Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can create a table in PDF document from DataTable in C# and VB.NET.

Steps to create table in PDF from DataTable programmatically:

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

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Grid;
using System.Data;
using System.Drawing;

 

VB.NET

Imports System.Drawing
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Grid

 

  1. Use the following code snippet to create table in a PDF file from DataTable.

C#

//Create a new PDF document
using (PdfDocument doc = new PdfDocument())
{
  //Add a page
  PdfPage page = doc.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");
 
  //Add rows to the DataTable
  dataTable.Rows.Add(new object[] { "E01", "Clay" });
  dataTable.Rows.Add(new object[] { "E02", "Thomas" });
  dataTable.Rows.Add(new object[] { "E03", "George" });
  dataTable.Rows.Add(new object[] { "E04", "Stefan" });
  dataTable.Rows.Add(new object[] { "E05", "Mathew" });
 
  //Assign data source
  pdfGrid.DataSource = dataTable;
 
  //Draw grid to the page of PDF document
  pdfGrid.Draw(page, new PointF(10, 10));
 
  //Save the document
  doc.Save("Output.pdf");
}

 

VB.NET

'Create a new PDF document
Using doc As PdfDocument = New PdfDocument()
 
  'Add a page
  Dim page As PdfPage = doc.Pages.Add()
 
  'Create a PdfGri
  Dim pdfGrid As PdfGrid = New PdfGrid
 
  'Create a DataTable.
  Dim dataTable As DataTable = New DataTable
 
  'Add columns to the DataTable
  dataTable.Columns.Add("ID")
  dataTable.Columns.Add("Name")
 
  'Add rows to the DataTable
  dataTable.Rows.Add(New Object() {"E01", "Clay"})
  dataTable.Rows.Add(New Object() {"E02", "Thomas"})
  dataTable.Rows.Add(New Object() {"E03", "George"})
  dataTable.Rows.Add(New Object() {"E04", "Stefan"})
  dataTable.Rows.Add(New Object() {"E05", "Mathew"})
 
  'Assign data source
  pdfGrid.DataSource = dataTable
 
  'Draw grid to the page of PDF document
  pdfGrid.Draw(page, New PointF(10, 10))
 
  'Save the document
  doc.Save("Output.pdf")
 
End Using

 

A complete working sample can be downloaded from Create-PDF-Table using DataTable.zip

By executing the program, you will get the PDF document as follows. Create Table in PDF File using DataTable

Take a moment to peruse the documentation, where you can find other options like drawing right-to-left text and multi-column text, consuming TrueType fonts, Standards fonts, and CJK fonts. Also, the features like PDF form filling, extract text or images from PDF, and protect PDF documents with code examples.

Refer here to explore the rich set of Syncfusion Essential PDF features.

An online sample link to create table in a PDF file.

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
Comments
Sheryl
Aug 18, 2019

im using xamarin.forms and it's not working

void filebuttonHandle_Clicked(object sender, EventArgs e) {

        //Create a new PDF document.
        PdfDocument doc = new PdfDocument();

        //Add a page.
        PdfPage page = doc.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");

        //Add rows to the DataTable
        dataTable.Rows.Add(new object[] { "E01", "Clay" });
        dataTable.Rows.Add(new object[] { "E02", "Thomas" });
        dataTable.Rows.Add(new object[] { "E03", "George" });
        dataTable.Rows.Add(new object[] { "E04", "Stefan" });
        dataTable.Rows.Add(new object[] { "E05", "Mathew" });

        //Assign data source
        pdfGrid.DataSource = dataTable;

        //Draw grid to the page of PDF document
        pdfGrid.Draw(page, new PointF(10, 10));

        //Save the PDF document to stream.
        MemoryStream stream = new MemoryStream();
        doc.Save(stream);
        //Close the document.
        doc.Close(true);
        stream.Position = 0;
        //Save the stream as a file in the device and invoke it for viewing
        Xamarin.Forms.DependencyService.Get().SaveAndView("Output.pdf", "application/pdf", stream);

}

Reply
Sheryl
Aug 18, 2019

do you have a platform for xamarin.. i'm missing something

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