Lalit Bhudiya
Lalit Bhudiya

Reputation: 4408

iText PDF Rectangle fill with color

I want to fill rectangle with color. I used these code but not working.

PdfContentByte canvas = writer.getDirectContent();
canvas.rectangle(50, 600, 500, 40);
canvas.setColorFill(BaseColor.GRAY);
//canvas.setRGBColorFill(20, 50, 30);
//canvas.setCMYKColorFill(0, 0, 0, 75);
canvas.setColorStroke(BaseColor.LIGHT_GRAY);
canvas.stroke();

Anyone has idea how to fill it ?

Upvotes: 1

Views: 9375

Answers (2)

Alexandre Campos
Alexandre Campos

Reputation: 290

I use iText 5 and this works for me

rectangle.setBackgroundColor(BaseColor.YELLOW); // or whatever color you have
canvas.rectangle(rectangle);

Just make sure it's not inside canvas.beginText() and canvas.endText() or any other drawing operation.

Upvotes: 1

Giovanni
Giovanni

Reputation: 543

You have to call canvas.fill() after setColorFill()

 PdfContentByte canvas = writer.getDirectContent();
 canvas.rectangle(50, 600, 500, 40);
 canvas.setColorFill(BaseColor.GRAY);
 canvas.fill();

Upvotes: 6

Related Questions