Articles in this section
Category / Section

How to format cells in a PDF Table?

3 mins read

Format cells in PDF table

All the cells in a PDF Grid have a style object associated with them. The properties of the style object can be set as shown in the code snippet below.

C#

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

// Add a page.
PdfPage page = doc.Pages.Add();

// Acquire page's graphics.
PdfGraphics graphics = page.Graphics;

// Create a PdfGrid.
PdfGrid table = 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" });

// Assign data source.
table.DataSource = dataTable;

// To set the background color.
table.Rows[1].Cells[1].Style.BackgroundBrush = Color.Red;

// Alignment of the text inside the cell.
table.Rows[1].Cells[0].Style.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 8f);
table.Rows[1].Cells[1].Style.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
table.Rows[1].Cells[2].Style.StringFormat = new PdfStringFormat(PdfTextAlignment.Right);
table.Rows[1].Cells[0].Style.StringFormat = new PdfStringFormat(PdfTextAlignment.Left);
table.Draw(page, new PointF(10, 10));

// Save the document.
doc.Save("Output.pdf");

// Close the document.
doc.Close(true);

 

Sample link:

https://www.syncfusion.com/downloads/support/directtrac/general/ze/PdfGridStyle-1517197644


Note:

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.

 

Conclusion

I hope you enjoyed learning about how to format cells in a PDF Table.

You can refer to our WinForms PDF’s feature tour page to know about its other groundbreaking feature representations. You can also 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 comments 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
Please  to leave a comment
Access denied
Access denied