gpt4 book ai didi

java - 无法获取填写 PDFBox 的路径

转载 作者:行者123 更新时间:2023-12-02 08:48:18 25 4
gpt4 key购买 nike

我正在使用 PDF 框尝试创建一些具有弯曲边缘的矩形。最终弄清楚如何使用贝塞尔曲线后,我能够得到我喜欢的形状。我现在的问题是我不知道如何填写它。我尝试过在随机点闭合路径,仅使用贝塞尔曲线绘制形状,在随机点抚摸路径,在随机点闭合路径,但它仍然不会填充整个物体。它似乎只填充了曲线的圆形边缘。谁能让我知道我做错了什么?非常感谢。生成器是一个对象,我用它来获取页面中当前设置的水平和垂直位置。在此示例中,水平和垂直位置值没有改变(水平位置为 200,垂直位置为 240)。这是我正在使用的代码(抱歉其中的魔数(Magic Number))

这是 PDF 页面中生成的图像,由于某种原因不会填充:

resulting image in the PDF page, which won't fill in for some reason

//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生成器对象,所以我使用了两个float变量xy 相反。)

结果如所期望:

screen shot

关于java - 无法获取填写 PDFBox 的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60942642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com