How to perform column customization in WinForms PDF table using C#?
The Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can perform the column customization in a PDF table using C# and VB.NET.
Steps to perform the column customization in a PDF table programmatically:
- 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.Graphics; using Syncfusion.Pdf.Grid; using System.Drawing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics Imports Syncfusion.Pdf.Grid Imports System.Drawing
- Use the following code sample to perform the column customization in a PDF table.
C#
//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(); //Include columns to the DataTable dataTable.Columns.Add("Name"); dataTable.Columns.Add("Age"); dataTable.Columns.Add("Sex"); dataTable.Columns.Add("Job"); //Include rows to the DataTable for (int i = 0; i < 5; i++) { dataTable.Rows.Add(new string[] { "John", "21", "Male", "Carpenter" }); dataTable.Rows.Add(new string[] { "Caty", "24", "Female", "Developer" }); dataTable.Rows.Add(new string[] { "Eddie", "32", "Male", "Reporter" }); dataTable.Rows.Add(new string[] { "Tony", "35", "Male", "Mechanic" }); dataTable.Rows.Add(new string[] { "Monica", "25", "Female", "Cook" }); } //Assign data source pdfGrid.DataSource = dataTable; pdfGrid.BeginCellLayout += PdfGrid_BeginCellLayout; //Apply the built-in table style pdfGrid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable4Accent1); //Draw grid to the page of PDF document pdfGrid.Draw(page, new PointF(10, 10)); //Save the document doc.Save("Output.pdf"); //close the document doc.Close(true); //This will open the PDF file and the result will be seen in the default PDF Viewer Process.Start("Output.pdf");
Event Handler:
private static void PdfGrid_BeginCellLayout(object sender, PdfGridBeginCellLayoutEventArgs args) { //Set the font PdfStandardFont font = new PdfStandardFont(PdfFontFamily.Courier, 12, PdfFontStyle.Bold); //Change the column font size if (args.CellIndex == 0) { args.Style.Font = font; } }
VB.NET
'Create a new PDF document Dim doc As PdfDocument = New PdfDocument() 'Add a page Dim page As PdfPage = doc.Pages.Add() 'Create a PdfGrid Dim pdfGrid As PdfGrid = New PdfGrid() 'Create a DataTable Dim dataTable As DataTable = New DataTable() 'Include columns to the DataTable dataTable.Columns.Add("Name") dataTable.Columns.Add("Age") dataTable.Columns.Add("Sex") dataTable.Columns.Add("Job") 'Include rows to the DataTable For i As Integer = 0 To 5 - 1 dataTable.Rows.Add(New String() {"John", "21", "Male", "Carpenter"}) dataTable.Rows.Add(New String() {"Caty", "24", "Female", "Developer"}) dataTable.Rows.Add(New String() {"Eddie", "32", "Male", "Reporter"}) dataTable.Rows.Add(New String() {"Tony", "35", "Male", "Mechanic"}) dataTable.Rows.Add(New String() {"Monica", "25", "Female", "Cook"}) Next 'Assign data source pdfGrid.DataSource = dataTable 'Call event handler to customize the column AddHandler pdfGrid.BeginCellLayout, AddressOf PdfGrid_BeginCellLayout 'Apply the built-in table style pdfGrid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable4Accent1) 'Draw grid to the page of PDF document pdfGrid.Draw(page, New PointF(10, 10)) 'Save the document doc.Save("Output.pdf") 'Close the document doc.Close(True) 'This will open the PDF file and the result will be seen in the default PDF Viewer Process.Start("Output.pdf")
Event Handler:
Private Sub PdfGrid_BeginCellLayout(ByVal sender As Object, ByVal args As PdfGridBeginCellLayoutEventArgs) 'Set the font Dim font As PdfStandardFont = New PdfStandardFont(PdfFontFamily.Courier, 12, PdfFontStyle.Bold) If args.CellIndex = 0 Then 'Change the column font size args.Style.Font = font End If End Sub
A complete working sample can be download from TableSample.zip.
By executing the program, you will get the output document as follows,
Take a moment to peruse the documentation. You can find other options like create a simple table using the PdfLightTable and PdfGrid, cell, row, and column customization using the PdfLightTable and PdfGrid, table customization, paginating table, built-in table styles, nested table, string formatting in a table, row, and column spanning.
Refer to this link to explore a rich set of Syncfusion Essential® PDF features.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or the NuGet feed, include a license key in your product. Refer to this link to learn about generating and registering the Syncfusion® license key in your application to use the components without trail message.
See Also:
Draw table in PDF document using PdfLightTable
Create a table in PDF file from datatable using PdfGrid
Create a simple table in PDF using ienumerable in Xamarin Forms
Conclusion
I hope you enjoyed learning about how to perform column customization in the WinForms PDF table using C#.
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!