IText Pdf生成中的矩形重叠 [英] Rectangle Overlapping in IText Pdf Generating

查看:336
本文介绍了IText Pdf生成中的矩形重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在图像中创建矩形,当我尝试使用坐标创建矩形时,
两个矩形一个接一个地放置。

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.

当我为两个生成一个矩形的矩形给出坐标时除此之外,我希望它们在图像中重叠。我怎样才能做到?

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);

推荐答案

请看一下 Rectangles 示例,了解如何创建一个看起来像 rectangles.pdf

< img src =https://i.stack.imgur.com/Bx0HV.pngalt =在此输入图像说明>

创建矩形时,你需要矩形的左下角和右上角的坐标。例如:

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;

您已经知道需要 PdfContentByte 用于绘制第一个矩形的实例:

You already know that you need a PdfContentByte instance to draw the first rectangle:

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);

为清楚起见,我定义了背景颜色,并将边框宽度设置为1 pt。

For clarity, I have defined a background color and I've set the border width to 1 pt.

现在,如果要添加一个与问题中描述的方法重叠的额外矩形,则需要更改 llx ury 值。那是基础数学。例如:

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. 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);

为了确保您看到差异,我现在使用了另一种背景颜色,我定义了0.5 pt作为边框宽度,白色作为边框颜色。

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.

它没有比这更简单。

这篇关于IText Pdf生成中的矩形重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆