Articles in this section
Category / Section

How to convert CSV to PDF table using C# and VB.NET?

8 mins read

Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can convert CSV (Comma Separate Values) file to PDF table in C# and VB.NET.

Steps to convert CSV to PDF table programmatically:

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

C#

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

 

VB.NET

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

 

  1. Use the following code snippet to convert CSV to PDF table.

C#

//Instantiate the spreadsheet creation engine
ExcelEngine excelEngine = new ExcelEngine();
//Instantiate the Excel application object
IApplication application = excelEngine.Excel;
//Loads or open an existing workbook through Open method of IWorkbooks
IWorkbook workbook = application.Workbooks.Open("Input.csv");
//Accessing via index
IWorksheet worksheet = workbook.Worksheets[0];
//Create a new PDF document
PdfDocument document = new PdfDocument();
//Add a page to the document
PdfPage page = document.Pages.Add();
//Create a new PdfGrid
PdfGrid pdfGrid = new PdfGrid();
//Add columns
pdfGrid.Columns.Add(worksheet.UsedRange.LastColumn);
pdfGrid.Headers.Add(1);
PdfGridRow pdfGridHeader = pdfGrid.Headers[0];
for (int i=0;i<worksheet.UsedRange.LastColumn;i++)
{
    pdfGridHeader.Cells[i].Value = worksheet.UsedRange.Columns[i].DisplayText;
}
//Add rows
for (int row = 2; row <= worksheet.UsedRange.LastRow; row++)
{ 
    PdfGridRow pdfGridRow = pdfGrid.Rows.Add();
    for (int col = 1; col <= worksheet.UsedRange.LastColumn; col++)
    {
        string text = worksheet[row, col].Text;
        pdfGridRow.Cells[col - 1].Value = text;
    }
}
//Apply built-in style
pdfGrid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable5DarkAccent6);
//Draw the PdfGrid
pdfGrid.Draw(page, PointF.Empty);
//Save the document 
document.Save("Table.pdf");
//Close the document
document.Close(true);
//This will open the PDF file so, the result will be seen in default PDF viewer
System.Diagnostics.Process.Start("Table.pdf");

 

VB.NET

'Instantiate the spreadsheet creation engine
Dim excelEngine As New ExcelEngine()
'Instantiate the Excel application object
Dim application As IApplication = excelEngine.Excel
'Loads or open an existing workbook through Open method of IWorkbooks
Dim workbook As IWorkbook = application.Workbooks.Open("Input.csv")
'Accessing via index
Dim worksheet As IWorksheet = workbook.Worksheets(0)
'Create a new PDF document
Dim document As New PdfDocument()
'Add a page to the document
Dim page As PdfPage = document.Pages.Add()
'Create a new PdfGrid
Dim pdfGrid As New PdfGrid()
'Add columns
pdfGrid.Columns.Add(worksheet.UsedRange.LastColumn)
pdfGrid.Headers.Add(1)
Dim pdfGridHeader As PdfGridRow = pdfGrid.Headers(0)
For i As Integer = 0 To worksheet.UsedRange.LastColumn - 1
    pdfGridHeader.Cells(i).Value = worksheet.UsedRange.Columns(i).DisplayText
Next
'Add rows
For row As Integer = 2 To worksheet.UsedRange.LastRow
    Dim pdfGridRow As PdfGridRow = pdfGrid.Rows.Add()
    For col As Integer = 1 To worksheet.UsedRange.LastColumn
        Dim text As String = worksheet(row, col).Text
        pdfGridRow.Cells(col - 1).Value = text
    Next
Next
'Apply built-in style
pdfGrid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable5DarkAccent6)
'Draw the PdfGrid
pdfGrid.Draw(page, PointF.Empty)
'Save the document 
document.Save("Table.pdf")
'Close the document
document.Close(True)
'This will open the PDF file so, the result will be seen in default PDF viewer
System.Diagnostics.Process.Start("Table.pdf")

A complete working sample can be downloaded from CSVToPDFSample.zip.

By executing the program, you will get the PDF document as follows. Output document screenshot in WinForms PDF

Take a moment to peruse the documentation, where you can find features like Excel to PDFWord to PDF, and adding simple table in a PDF document with code examples.

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.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied