C# tutorial: customize border of cells of a table in PDF


Customize border of cells of a table in PDF

In the previous page, you learnt to customize the background color of rows of the table by overriding the TableLayout method of the IPdfPTableEvent interface. In this tutorial, you will learn to customize the borders of cells of the table. For example, suppose you want to apply the round rectangle shape to all cells of the table except the cells in the head row as shown below.

round rectangle cells

To achieve this goal, you need to implement the IPdfPCellEvent interface. The IPdfPCellEvent interface has one method called CellLayout that must be overridden. The CellLayout method has three parameters:

- cell—The PdfPCell object to which the event is added. It is read-only.
- rect—The Rectangle object defining the borders of the cell.
- canvas—An array of PdfContentByte objects that allow you to draw text or add shapes to the cell.

class CustomCell: IPdfPCellEvent
{
public void CellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] canvas)
{

PdfContentByte cb = canvas[PdfPTable.BACKGROUNDCANVAS];
cb.SetColorStroke(new BaseColor(200,0,0));
cb.SetLineWidth(2);
cb.RoundRectangle(rect.Left, rect.Bottom, rect.Width,rect.Height,3);
cb.Stroke();

}
}

 

Now you have the CustomBorder class that implements the IPdfPCellEvent interface. Next, you need to create an object of CustomBorder class and assign it to the CellEvent property of PdfPCell object. See the example code below.


CustomCell cusborder = new CustomCell();
cellid.CellEvent = cusborder;

You can download the complete C# code of this tutorial from here.


Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.