简体   繁体   中英

Fill Color in Rectangle of iText PDF

I am making a project in JAVA in which i am working with iText. Now on one stage i want to add color to my canvas i tried following codes but they are not working

 PdfContentByte canvas = pdf.getDirectContent();
      Rectangle rect = new Rectangle(12,12,583,830);
           rect.setBorder(Rectangle.BOX);
           rect.setBorderWidth(1);
            canvas.rectangle(rect);
            canvas.setColorFill(BaseColor.YELLOW);
            canvas.fillStroke();

All things are working fine except Color.

You are mixing 2 different things.

Either you define the background color of the Rectangle object:

rect.setBackgroundColor(BaseColor.YELLOW);

Or you use the setColorFill() method (as you do), but in that case you need to use the rectangle() method with the coordinates of the rectangle as parameters to construct the path.

Note that using setFillStroke() makes sense in the latter case, but it doesn't make sense in the former case. In your code sample, it doesn't make sense because you're using a Rectangle object.

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