无法获取填写 PDFBox 的路径
Can't Get Path to Fill in PDFBox
我正在使用 PDF 框尝试创建一些具有弯曲边缘的矩形。在最终弄清楚如何使用贝塞尔曲线之后,我能够得到我喜欢的形状。我现在的问题是我不知道如何填写它。我试过在随机点关闭路径,仅使用贝塞尔曲线绘制形状,在随机点抚摸路径,在随机点关闭路径,但它仍然不会填满整个东西。它似乎只填充曲线的圆形边缘。谁能让我知道我做错了什么?非常感谢。 generator 是我用来获取页面中当前设置的水平和垂直位置的对象。本例中水平和垂直位置值不变(水平位置为 200,垂直位置为 240)。这是我正在使用的代码(抱歉其中的神奇数字)
这是 PDF 页面中生成的图像,由于某种原因无法填充:
//Creating PDF document object
PDDocument document = new PDDocument();
//Creating the PDDocumentInformation object
PDDocumentInformation pdd = document.getDocumentInformation();
generator.drawRectangleWithCurvedBorders(200, 400, cs, generator);
public void drawRectangleWithCurvedBorders(int width, int height, PDPageContentStream contentStream, XClass generator) throws IOException
{
contentStream.setStrokingColor( Color.BLACK );
contentStream.setNonStrokingColor( Color.BLACK );
// bottom of rectangle
contentStream.moveTo(generator.getHorizontalPosition() - 0.5f, generator.getVerticalPosition() );
contentStream.lineTo(generator.getHorizontalPosition() + width + 0.5f, generator.getVerticalPosition() );
contentStream.moveTo(generator.getHorizontalPosition() + width, generator.getVerticalPosition() );
contentStream.curveTo(generator.getHorizontalPosition() + width + 5.9f, generator.getVerticalPosition() + 0.14f,
generator.getHorizontalPosition() + width + 11.06f, generator.getVerticalPosition() + 5.16f,
generator.getHorizontalPosition() + width + 10.96f, generator.getVerticalPosition() + 10);
// left of rectangle
contentStream.moveTo(generator.getHorizontalPosition(), generator.getVerticalPosition() );
contentStream.curveTo(generator.getHorizontalPosition() - 5.9f, generator.getVerticalPosition() + 0.14f,
generator.getHorizontalPosition() - 11.06f, generator.getVerticalPosition() + 5.16f,
generator.getHorizontalPosition() - 10.96f, generator.getVerticalPosition() + 10);
contentStream.moveTo(generator.getHorizontalPosition() - 10.96f, generator.getVerticalPosition() + 10 - 0.5f);
contentStream.lineTo(generator.getHorizontalPosition() - 10.96f, generator.getVerticalPosition() + height + 0.5f );
// right of rectangle
contentStream.moveTo(generator.getHorizontalPosition() + width + 10.96f, generator.getVerticalPosition() + 10 - 0.5f);
contentStream.lineTo(generator.getHorizontalPosition() + width + 10.96f, generator.getVerticalPosition() + height + 0.5f);
contentStream.moveTo(generator.getHorizontalPosition() + width, generator.getVerticalPosition() + height + 10);
contentStream.curveTo(generator.getHorizontalPosition() + width + 5.9f, generator.getVerticalPosition() + height + 0.14f + 10,
generator.getHorizontalPosition() + width + 11.06f, generator.getVerticalPosition() + height - 5.16f + 10,
generator.getHorizontalPosition() + width + 10.96f, generator.getVerticalPosition() + height);
// top of rectangle
contentStream.moveTo(generator.getHorizontalPosition() + width + 0.5f, generator.getVerticalPosition() + height + 10);
contentStream.lineTo(generator.getHorizontalPosition() - 0.5f, generator.getVerticalPosition() + height + 10);
contentStream.moveTo(generator.getHorizontalPosition(), generator.getVerticalPosition() + height + 10);
contentStream.curveTo(generator.getHorizontalPosition() - 5.9f, generator.getVerticalPosition() + height + 0.14f + 10,
generator.getHorizontalPosition() - 11.06f, generator.getVerticalPosition() + height - 5.16f + 10,
generator.getHorizontalPosition() - 10.96f, generator.getVerticalPosition() + height);
contentStream.closePath();
contentStream.fill();
}
正如 Tilman 在对该问题的评论中所说的那样,问题在于许多 moveTo()
指令。事实上,每个 moveTo
开始一个新的子路径,每个子路径都意味着一个单独的填充区域。根据填充变体和子路径方向,这些子路径的交叉点实际上可能 被排除 被填充。
因此,要创建一个具有 弯曲边框 的填充矩形,如 OP 所述,应该重新排列路径构建说明,以便它们勾勒出没有 moveTo
中断流程,例如像这样:
try ( PDDocument document = new PDDocument() ) {
PDPage page = new PDPage();
document.addPage(page);
try ( PDPageContentStream contentStream = new PDPageContentStream(document, page) ) {
float x = 100;
float y = 100;
float width = 200;
float height = 300;
contentStream.setStrokingColor( Color.BLACK );
contentStream.setNonStrokingColor( Color.BLACK );
contentStream.moveTo(x, y);
// bottom of rectangle, left to right
contentStream.lineTo(x + width, y );
contentStream.curveTo(x + width + 5.9f, y + 0.14f,
x + width + 11.06f, y + 5.16f,
x + width + 10.96f, y + 10);
// right of rectangle, bottom to top
contentStream.lineTo(x + width + 10.96f, y + height);
contentStream.curveTo(x + width + 11.06f, y + height - 5.16f + 10,
x + width + 5.9f, y + height + 0.14f + 10,
x + width, y + height + 10);
// top of rectangle, right to left
contentStream.lineTo(x, y + height + 10);
contentStream.curveTo(x - 5.9f, y + height + 0.14f + 10,
x - 11.06f, y + height - 5.16f + 10,
x - 10.96f, y + height);
// left of rectangle, top to bottom
contentStream.lineTo(x - 10.96f, y + 10);
contentStream.curveTo(x - 11.06f, y + 5.16f,
x - 5.9f, y + 0.14f,
x, y);
contentStream.closePath();
contentStream.fill();
}
document.save(new File("CurvedBorderRectangleLikeMaht33n-improved.pdf"));
}
(CurvedBorderRectangle 测试 testLikeMaht33nImproved
)
(我没有那个 XClass generator
对象,所以我用两个 float
变量 x
和 y
代替。)
结果如愿:
我正在使用 PDF 框尝试创建一些具有弯曲边缘的矩形。在最终弄清楚如何使用贝塞尔曲线之后,我能够得到我喜欢的形状。我现在的问题是我不知道如何填写它。我试过在随机点关闭路径,仅使用贝塞尔曲线绘制形状,在随机点抚摸路径,在随机点关闭路径,但它仍然不会填满整个东西。它似乎只填充曲线的圆形边缘。谁能让我知道我做错了什么?非常感谢。 generator 是我用来获取页面中当前设置的水平和垂直位置的对象。本例中水平和垂直位置值不变(水平位置为 200,垂直位置为 240)。这是我正在使用的代码(抱歉其中的神奇数字)
这是 PDF 页面中生成的图像,由于某种原因无法填充:
//Creating PDF document object
PDDocument document = new PDDocument();
//Creating the PDDocumentInformation object
PDDocumentInformation pdd = document.getDocumentInformation();
generator.drawRectangleWithCurvedBorders(200, 400, cs, generator);
public void drawRectangleWithCurvedBorders(int width, int height, PDPageContentStream contentStream, XClass generator) throws IOException
{
contentStream.setStrokingColor( Color.BLACK );
contentStream.setNonStrokingColor( Color.BLACK );
// bottom of rectangle
contentStream.moveTo(generator.getHorizontalPosition() - 0.5f, generator.getVerticalPosition() );
contentStream.lineTo(generator.getHorizontalPosition() + width + 0.5f, generator.getVerticalPosition() );
contentStream.moveTo(generator.getHorizontalPosition() + width, generator.getVerticalPosition() );
contentStream.curveTo(generator.getHorizontalPosition() + width + 5.9f, generator.getVerticalPosition() + 0.14f,
generator.getHorizontalPosition() + width + 11.06f, generator.getVerticalPosition() + 5.16f,
generator.getHorizontalPosition() + width + 10.96f, generator.getVerticalPosition() + 10);
// left of rectangle
contentStream.moveTo(generator.getHorizontalPosition(), generator.getVerticalPosition() );
contentStream.curveTo(generator.getHorizontalPosition() - 5.9f, generator.getVerticalPosition() + 0.14f,
generator.getHorizontalPosition() - 11.06f, generator.getVerticalPosition() + 5.16f,
generator.getHorizontalPosition() - 10.96f, generator.getVerticalPosition() + 10);
contentStream.moveTo(generator.getHorizontalPosition() - 10.96f, generator.getVerticalPosition() + 10 - 0.5f);
contentStream.lineTo(generator.getHorizontalPosition() - 10.96f, generator.getVerticalPosition() + height + 0.5f );
// right of rectangle
contentStream.moveTo(generator.getHorizontalPosition() + width + 10.96f, generator.getVerticalPosition() + 10 - 0.5f);
contentStream.lineTo(generator.getHorizontalPosition() + width + 10.96f, generator.getVerticalPosition() + height + 0.5f);
contentStream.moveTo(generator.getHorizontalPosition() + width, generator.getVerticalPosition() + height + 10);
contentStream.curveTo(generator.getHorizontalPosition() + width + 5.9f, generator.getVerticalPosition() + height + 0.14f + 10,
generator.getHorizontalPosition() + width + 11.06f, generator.getVerticalPosition() + height - 5.16f + 10,
generator.getHorizontalPosition() + width + 10.96f, generator.getVerticalPosition() + height);
// top of rectangle
contentStream.moveTo(generator.getHorizontalPosition() + width + 0.5f, generator.getVerticalPosition() + height + 10);
contentStream.lineTo(generator.getHorizontalPosition() - 0.5f, generator.getVerticalPosition() + height + 10);
contentStream.moveTo(generator.getHorizontalPosition(), generator.getVerticalPosition() + height + 10);
contentStream.curveTo(generator.getHorizontalPosition() - 5.9f, generator.getVerticalPosition() + height + 0.14f + 10,
generator.getHorizontalPosition() - 11.06f, generator.getVerticalPosition() + height - 5.16f + 10,
generator.getHorizontalPosition() - 10.96f, generator.getVerticalPosition() + height);
contentStream.closePath();
contentStream.fill();
}
正如 Tilman 在对该问题的评论中所说的那样,问题在于许多 moveTo()
指令。事实上,每个 moveTo
开始一个新的子路径,每个子路径都意味着一个单独的填充区域。根据填充变体和子路径方向,这些子路径的交叉点实际上可能 被排除 被填充。
因此,要创建一个具有 弯曲边框 的填充矩形,如 OP 所述,应该重新排列路径构建说明,以便它们勾勒出没有 moveTo
中断流程,例如像这样:
try ( PDDocument document = new PDDocument() ) {
PDPage page = new PDPage();
document.addPage(page);
try ( PDPageContentStream contentStream = new PDPageContentStream(document, page) ) {
float x = 100;
float y = 100;
float width = 200;
float height = 300;
contentStream.setStrokingColor( Color.BLACK );
contentStream.setNonStrokingColor( Color.BLACK );
contentStream.moveTo(x, y);
// bottom of rectangle, left to right
contentStream.lineTo(x + width, y );
contentStream.curveTo(x + width + 5.9f, y + 0.14f,
x + width + 11.06f, y + 5.16f,
x + width + 10.96f, y + 10);
// right of rectangle, bottom to top
contentStream.lineTo(x + width + 10.96f, y + height);
contentStream.curveTo(x + width + 11.06f, y + height - 5.16f + 10,
x + width + 5.9f, y + height + 0.14f + 10,
x + width, y + height + 10);
// top of rectangle, right to left
contentStream.lineTo(x, y + height + 10);
contentStream.curveTo(x - 5.9f, y + height + 0.14f + 10,
x - 11.06f, y + height - 5.16f + 10,
x - 10.96f, y + height);
// left of rectangle, top to bottom
contentStream.lineTo(x - 10.96f, y + 10);
contentStream.curveTo(x - 11.06f, y + 5.16f,
x - 5.9f, y + 0.14f,
x, y);
contentStream.closePath();
contentStream.fill();
}
document.save(new File("CurvedBorderRectangleLikeMaht33n-improved.pdf"));
}
(CurvedBorderRectangle 测试 testLikeMaht33nImproved
)
(我没有那个 XClass generator
对象,所以我用两个 float
变量 x
和 y
代替。)
结果如愿: