简体   繁体   English

IText Pdf生成中的矩形重叠

[英]Rectangle Overlapping in IText Pdf Generating

i tried to create rectangles as in the image, when i tried to create rectangles using coordinates the two rectangles are placing one after other. 我试图像图像中那样创建矩形,当我尝试使用坐标创建矩形时,两个矩形又一个接一个地放置。

Here is the code how iam creating Rectangle. 这是代码如何创建Rectangle的代码。

when i give coordinates for two rectangles those are generating one after other, i want them to overlap as in the image..How can i make it? 当我给出两个矩形的坐标时,它们要一个接一个地生成,我希望它们像图像中那样重叠。

                PdfWriter writer= PdfWriter.getInstance(document, new FileOutputStream(filename));
                document.open();

                PdfContentByte cb = writer.getDirectContent();
                Rectangle rect,rect1;
                rect = new Rectangle(p1,p2,p3,p4); // CO-ORDINATES OF RECTANGLE
                rect.setBorder(Rectangle.BOX);
                cb.rectangle(rect);

在此处输入图片说明

Please take a look at the Rectangles example to find out how to create a PDF that looks like rectangles.pdf : 请看一下“ Rectangles示例,以了解如何创建看起来像矩形的PDF:

在此处输入图片说明

When creating a rectangle, you need the coordinates of the lower-left corner and the upper-right corner of the rectangle. 创建矩形时,需要矩形的左下角和右上角的坐标。 For instance: 例如:

float llx = 36;
float lly = 700;
float urx = 200;
float ury = 806;

You already know that you need a PdfContentByte instance to draw the first rectangle: 您已经知道需要一个PdfContentByte实例来绘制第一个矩形:

PdfContentByte canvas = writer.getDirectContent();
Rectangle rect1 = new Rectangle(llx, lly, urx, ury);
rect1.setBackgroundColor(BaseColor.LIGHT_GRAY);
rect1.setBorder(Rectangle.BOX);
rect1.setBorderWidth(1);
canvas.rectangle(rect1);

For clarity, I have defined a background color and I've set the border width to 1 pt. 为了清楚起见,我定义了背景色,并将边框宽度设置为1 pt。

Now when you want to add an extra rectangle that overlaps the same way as described in your question, you need to change the llx and ury value. 现在,当你想添加重叠在你的问题中所述相同的方法,额外的矩形,你需要改变llxury值。 That's elementary math. 那是基础数学。 For instance: 例如:

Rectangle rect2 = new Rectangle(llx + 60, lly, urx, ury - 40);
rect2.setBackgroundColor(BaseColor.DARK_GRAY);
rect2.setBorder(Rectangle.BOX);
rect2.setBorderColor(BaseColor.WHITE);
rect2.setBorderWidth(0.5f);
canvas.rectangle(rect2);

To make sure you see the difference, I've now used another background color, and I defined 0.5 pt as the border width and white as the border color. 为了确保您看到差异,我现在使用了另一种背景色,并将边框宽度定义为0.5 pt,将边框颜色定义为白色。

It doesn't get any simpler than this. 没有比这更简单的了。

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

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