Don Diego
Don Diego

Reputation: 1508

iText7 and C#, put a filled shape in a table cell

I am trying to add a rectangle with rounded borders in a cell of a table, created with iText7 and C#.

I tried using

table.AddCell(new Cell().Add(rect)

where i created rect with

Rectangle boundingBox = new Rectangle(20, 470, 30, 30);
PdfFormXObject xObject = new PdfFormXObject(boundingBox);
xObject.MakeIndirect(pdfDoc); //Make sure the XObject gets added to the document
PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage());
Color greenColor = new DeviceCmyk(100, 30, 100, 0);
canvas.SetFillColor(greenColor);
canvas.Rectangle(294, 780, 50, 35);
canvas.FillStroke();

Image rect = new Image(xObject);

suggested from a friend of mine, but I think this is the wrong way to do this, and I'm not even quite sure of what this code does. Plus, the rectangle is transparent, has big margins, and the font in the cells is now green too (it was black before inserting the rectangle).

Here is how it looks like (i purposedly put the square a little bit higher to show the transparency):

enter image description here

What I want to do is to create a green shape rectangle, round the borders, then put it in the cell.

It should look like this:

enter image description here

Is there a good way to do that?

Upvotes: 0

Views: 1093

Answers (1)

Alexey Subach
Alexey Subach

Reputation: 12302

You can create a block-level layout object (Div) and set all the necessary visual properties to it. It is not necessary to perform custom drawing operations. Here is the code sample (in Java, but converting to C# boils down to capitalizing method names):

Div rectangle = new Div()
        .setHeight(30)
        .setWidth(30)
        .setBackgroundColor(ColorConstants.GREEN)
        .setBorderRadius(new BorderRadius(5))
        .setBorder(new SolidBorder(ColorConstants.GREEN, 1));

table.addCell(new Cell().add(rectangle));

Visually the result looks like this: result

Upvotes: 4

Related Questions