How to Convert CSV to PDF Table Using C# and VB.NET in WinForms?
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:
- Create a new C# console application project.
- Install the Syncfusion.ExcelToPdfConverter.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- 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
- 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.
Take a moment to peruse the documentation, where you can find features like Excel to PDF, Word 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.
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.
Conclusion
I hope you enjoyed learning how to convert CSV to PDF table using C# and VB.NET in WinForms.
You can refer to our WinForms PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms PDF example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!