简体   繁体   English

在iText PDF的矩形中填充颜色

[英]Fill Color in Rectangle of iText PDF

I am making a project in JAVA in which i am working with iText. 我正在用iText在JAVA中制作一个项目。 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. 您正在混合2种不同的东西。

Either you define the background color of the Rectangle object: 您可以定义Rectangle对象的背景色:

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. 或者,您也可以使用setColorFill()方法(如您所做的那样),但是在那种情况下,您需要使用以rectangle()坐标为参数的rectangle()方法来构造路径。

Note that using setFillStroke() makes sense in the latter case, but it doesn't make sense in the former case. 注意,在后一种情况下使用setFillStroke()是有意义的,但在前一种情况下则没有意义。 In your code sample, it doesn't make sense because you're using a Rectangle object. 在您的代码示例中,这没有意义,因为您使用的是Rectangle对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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