Articles in this section
Category / Section

How to format cells in a PDF Table?

1 min read

Format cells in PDF table

All the cells in a PDF Grid have a style object associated with it. 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 backcolor.
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

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