Java Can';无法获取填充PDFBox的路径

Java Can';无法获取填充PDFBox的路径,java,pdf,pdfbox,Java,Pdf,Pdfbox,我正在使用PDF框尝试创建一些带有曲线边的矩形。在最终弄明白如何使用贝齐尔曲线后,我得到了一个我喜欢的形状。我现在的问题是,我不知道如何填补它。我尝试过在随机点关闭路径,仅使用贝塞尔曲线绘制形状,在随机点抚摸路径,在随机点关闭路径,但仍然无法填充整个过程。它似乎只填充曲线的圆边。谁能告诉我我做错了什么?非常感谢。生成器是我用来获取页面中当前设置的水平和垂直位置的对象。在本例中,水平和垂直位置值不变(水平位置为200,垂直位置为240)。这是我正在使用的代码(很抱歉这里有神奇的数字) 以下是PDF

我正在使用PDF框尝试创建一些带有曲线边的矩形。在最终弄明白如何使用贝齐尔曲线后,我得到了一个我喜欢的形状。我现在的问题是,我不知道如何填补它。我尝试过在随机点关闭路径,仅使用贝塞尔曲线绘制形状,在随机点抚摸路径,在随机点关闭路径,但仍然无法填充整个过程。它似乎只填充曲线的圆边。谁能告诉我我做错了什么?非常感谢。生成器是我用来获取页面中当前设置的水平和垂直位置的对象。在本例中,水平和垂直位置值不变(水平位置为200,垂直位置为240)。这是我正在使用的代码(很抱歉这里有神奇的数字)

以下是PDF页面中的结果图像,由于某些原因无法填充:


正如蒂尔曼在对该问题的评论中所说,问题在于大量的
moveTo()
指令。实际上,每个
moveTo
都会启动一个新的子路径,并且每个子路径都意味着一个单独的填充区域。根据填充变量和子路径方向,这些子路径的交点实际上可能被排除在填充之外

因此,要创建具有OP所概述的曲线边界的填充矩形,应重新排列路径构建指令,以便它们在不中断流程的情况下概述矩形,例如:

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"));
}
(测试
testLikeMaht33nImproved

(我没有
XClass generator
对象,所以我使用了两个
float
变量
x
y

结果如下:


我认为问题在于移动对象太多()。如果要绘制形状,则在开始处应该只有一个moveTo()。想想看——moveTo把你的笔举起,放到别处。其他的方法是把笔放下。尝试将其替换为lineTo。事实上,每个
moveTo
都会启动一个新的子路径,并且每个子路径都意味着一个单独的填充区域。maht33n,您是否尝试过@Tilman建议的方法?成功了吗?成功了!很抱歉反应太晚。谢谢大家的帮助。
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"));
}