iText:PdfContentByte.rectangle(Rectangle)的行为与预期不符 [英] iText: PdfContentByte.rectangle(Rectangle) does not behave as expected
问题描述
我正在使用iText 2.1.7,我试图在我的文档中绘制一个简单的矩形。下面的代码块按预期工作,并绘制一个矩形,覆盖整个页面,不包括页边距。
I am using iText 2.1.7 and I am trying to draw a simple rectangle to my document. The below block of code works as expected and draws a rectangle that covers the entire page not including the page margins.
Document document = new Document(PageSize.A4.rotate());
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
document.open();
PdfContentByte canvas = writer.getDirectContent();
Rectangle pageDimensions = writer.getPageSize();
canvas.saveState();
canvas.rectangle(
pageDimensions.getLeft(marginLeft),
pageDimensions.getBottom(marginBottom),
pageDimensions.getRight(marginRight),
pageDimensions.getTop(marginTop));
canvas.setColorStroke(Color.BLACK);
canvas.stroke();
canvas.restoreState();
document.close();
但是,如果我略微更改画布代码块,以便我在外面定义矩形PdfContentByte,然后我的代码生成一个空白页。
However, if I change the block of canvas code slightly such that I am defining the rectangle outside of the PdfContentByte, then my code generates a blank page.
...
Rectangle marginBox = new Rectangle(
pageDimensions.getLeft(marginLeft),
pageDimensions.getBottom(marginBottom),
pageDimensions.getRight(marginRight),
pageDimensions.getTop(marginTop));
canvas.saveState();
canvas.rectangle(marginBox);
canvas.setColorStroke(Color.BLACK);
canvas.stroke();
canvas.restoreState();
...
这不是的预期用途 PdfContentByte.rectangle(Rectangle)
方法??理想情况下,我想以一种与directContent没有紧密耦合的方式定义矩形(以及它们的边框颜色和宽度),并且可以在以后将它们添加到directContent中。
Is this not the intended usage of the PdfContentByte.rectangle(Rectangle)
method?? Ideally, I would like define Rectangles (along with their border colors and widths) in a way that is not so tightly coupled with the directContent and have the freedom to add them to the directContent at a later point.
推荐答案
首先,我觉得有必要说你正在使用你不应再使用的iText版本。有关详细信息,请参见 http://itextpdf.com/salesfaq 。
First, I feel obliged to say that you're using a version of iText you should no longer use. See http://itextpdf.com/salesfaq for more info.
至于你的问题,你做了一些错误的假设。例如:当你使用 Rectangle
对象时,使用 stroke()
运算符没有意义,因为 rectangle()
采用 Rectangle
作为参数描边路径的方法(与采用4 <$ c的方法相反) $ c> float values)。
As for your question, you are making some assumptions that are wrong. For instance: when you use the Rectangle
object, using the stroke()
operator doesn't make sense as the rectangle()
method that takes a Rectangle
as parameter strokes the path (as opposed to the method that takes 4 float
values).
// This draws and strokes the path:
canvas.rectangle(marginBox);
// There is no path to stroke anymore:
canvas.setColorStroke(Color.BLACK);
canvas.stroke();
矩形()
方法如果没有为 Rectangle
定义边框和边框宽度, Rectangle
将不会执行任何操作。换句话说,你需要这样的东西:
The rectangle()
method that takes a Rectangle
won't do anything if no border and no border width have been defined for the Rectangle
. In other words, you need something like this:
Rectangle rect = new Rectangle(36, 36, 559, 806);
rect.setBorder(Rectangle.BOX);
rect.setBorderWidth(2);
canvas.rectangle(rect);
如果没有定义边界,边界的宽度也不大于0,则假设存在没有边界。
If no borders are defined, nor a width greater than 0 for the border, it is assumed that there is no border.
这篇关于iText:PdfContentByte.rectangle(Rectangle)的行为与预期不符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!