Articles in this section
Category / Section

How to set styles in a PDF table cell using C# and VB.NET?

5 mins read

Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can set styles in a PDF table cell using C# and VB.NET.

Steps to set style in PDF table cell programmatically:

  1. Create a new C# console application project. Create a console application project in WinForms PDF
  2. Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org. NuGet package reference screenshot in WinForms PDF
  3. 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

 

  1. Use the following code snippet to set styles in PDF table cell.

C#

//Create a new PDF document
PdfDocument pdfDocument = new PdfDocument();
//Add a page to the document 
PdfPage pdfPage = pdfDocument.Pages.Add();
//Create a new PdfGrid
PdfGrid pdfGrid = new PdfGrid();
//Add three columns
pdfGrid.Columns.Add(3);
//Add header
pdfGrid.Headers.Add(1);
PdfGridRow pdfGridHeader = pdfGrid.Headers[0];
pdfGridHeader.Cells[0].Value = "Employee ID";
pdfGridHeader.Cells[1].Value = "Employee Name";
pdfGridHeader.Cells[2].Value = "Salary";
//Add style to header
pdfGridHeader.Style.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 15, PdfFontStyle.Bold);
//Add rows
PdfGridRow pdfGridRow = pdfGrid.Rows.Add();
pdfGridRow.Cells[0].Value = "E01";
pdfGridRow.Cells[1].Value = "Clay";
pdfGridRow.Cells[2].Value = "$10,000";
pdfGridRow.Height = 50;
//Specify the style for the PdfGridCell
PdfGridCellStyle pdfGridCellStyle = new PdfGridCellStyle();
pdfGridCellStyle.TextPen = PdfPens.Red;
pdfGridCellStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
pdfGridCellStyle.BackgroundImage = new PdfBitmap("../../Data/Support.jpg");
PdfGridCell pdfGridCell = pdfGrid.Rows[0].Cells[0];
//Apply style
pdfGridCell.Style = pdfGridCellStyle;
//Set image position for the background image in the style
pdfGridCell.ImagePosition = PdfGridImagePosition.Stretch;
//Draw the PdfGrid
pdfGrid.Draw(pdfPage, PointF.Empty);
//Save the document
pdfDocument.Save("Table.pdf");
//Close the document
pdfDocument.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

'Create a new PDF document
Dim pdfDocument As PdfDocument = New PdfDocument()
'Add a page to the document 
Dim pdfPage As PdfPage = pdfDocument.Pages.Add()
'Create a new PdfGrid
Dim pdfGrid As PdfGrid = New PdfGrid()
'Add three columns
pdfGrid.Columns.Add(3)
'Add header
pdfGrid.Headers.Add(1)
Dim pdfGridHeader As PdfGridRow = pdfGrid.Headers(0)
pdfGridHeader.Cells(0).Value = "Employee ID"
pdfGridHeader.Cells(1).Value = "Employee Name"
pdfGridHeader.Cells(2).Value = "Salary"
'Add style to header
pdfGridHeader.Style.Font = New PdfStandardFont(PdfFontFamily.TimesRoman, 15, PdfFontStyle.Bold)
'Add rows
Dim pdfGridRow As PdfGridRow = pdfGrid.Rows.Add()
pdfGridRow.Cells(0).Value = "E01"
pdfGridRow.Cells(1).Value = "Clay"
pdfGridRow.Cells(2).Value = "$10,000"
pdfGridRow.Height = 50
'Specify the style for the PdfGridCell
Dim pdfGridCellStyle As PdfGridCellStyle = New PdfGridCellStyle()
pdfGridCellStyle.TextPen = PdfPens.Red
pdfGridCellStyle.StringFormat = New PdfStringFormat(PdfTextAlignment.Center)
pdfGridCellStyle.BackgroundImage = New PdfBitmap("../../Data/Support.jpg")
Dim pdfGridCell As PdfGridCell = pdfGrid.Rows(0).Cells(0)
'Apply style
pdfGridCell.Style = pdfGridCellStyle
'Set image position for the background image in the style
pdfGridCell.ImagePosition = PdfGridImagePosition.Stretch
'Draw the PdfGrid
pdfGrid.Draw(pdfPage, PointF.Empty)
'Save the document
pdfDocument.Save("Table.pdf")
'Close the document
pdfDocument.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 CellStyleSample.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 other options like creating a table using PdfLightTable and PdfGrid, cell and row customization in PdfLightTable and PdfGrid, built-in table styles to PdfGrid and pagination in PdfGrid and PdfLightTable with code examples.

An online sample link to create the richly formatted table.

Refer here to learn more about PDF Tables in Essential® PDF library.

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.

 

Conclusion

 

Hope you enjoyed learning about how to set styles in a PDF table cell using C# and VB.NET?.

You can refer to our WinForms PDF’s feature tour page to learn about its other groundbreaking feature representations. You can explore our WinForms PDF documentation to understand how to present and manipulate data.

For current customers, you can check out our WinForms 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 WinForms PDF and other WinForms components.

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!

 


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