Add rectangle to pdfpcell itextsharp

How to add a rectangle with a specific width, height and background color in PdfPCell using itextsharp?

Something like that:

PdfPCell cell = new PdfPCell(); Rectangle rectangle = new Rectangle(); rectangle.Width = 50f; rectangle.BackgroundColor = BaseColor.RED; cell.AddElement(cell); 
+1
source share
1 answer

The simple answer is: draw a Rectangle as an XObject ( PdfTemplate ) object, wrap it inside the Image object and add this image to the table.

However: there are several ways to do this, and there can only be one way that will produce the desired result. That's why I made you an example: rectangle_in_cell.pdf

Take a close look at this PDF. On the upper border, you see a line that measures 120 pt in length. In different tables, you see three rectangles that were created as a 120 by 80 pt rectangle. It seems that only one rectangle has the correct size.

When you add objects to the table, iText often resizes the content to fit in the cell. The RectangleInCell example shows code differences between the three approaches. It is written in Java, but I'm sure you can adapt it to C #.

+1
source

All Articles