简体   繁体   中英

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 ?

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM