iText PDF Rectangle fill with color

拟墨画扇 提交于 2020-01-06 19:56:16

问题


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 ?


回答1:


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

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



回答2:


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.



来源:https://stackoverflow.com/questions/26162628/itext-pdf-rectangle-fill-with-color

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!