使用 PDFBox 绘制透明线

发布于 2024-10-09 04:20:55 字数 369 浏览 0 评论 0原文

我想在 PDFBox 中用透明线绘制线条和多边形。这是我如何绘制蓝线的一些示例代码,但我无法弄清楚如何更改颜色的 alpha 值。

PDDocument document = new PDDocument();  
PDPage page = new PDPage();  
document.addPage(page);  
PDPageContentStream contentStream = new PDPageContentStream(document, page);  
contentStream.setStrokingColor(66, 177, 230);  
contentStream.drawLine(100, 100, 200, 200);  

I would like to draw lines and polygons with transparent lines in PDFBox. Here is some sample code of how I am drawing a blue line, but I cannot figure out to change the alpha value of the color.

PDDocument document = new PDDocument();  
PDPage page = new PDPage();  
document.addPage(page);  
PDPageContentStream contentStream = new PDPageContentStream(document, page);  
contentStream.setStrokingColor(66, 177, 230);  
contentStream.drawLine(100, 100, 200, 200);  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

游魂 2024-10-16 04:20:55

从 PDFBox 2.0 开始,appendRawCommands 已弃用。

    float alpha = 0.5f;
    PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState();
    graphicsState.setStrokingAlphaConstant(alpha);
    stream.setGraphicsStateParameters(graphicsState);
    // draw line here

As of PDFBox 2.0 appendRawCommands is deprecated.

    float alpha = 0.5f;
    PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState();
    graphicsState.setStrokingAlphaConstant(alpha);
    stream.setGraphicsStateParameters(graphicsState);
    // draw line here
我的黑色迷你裙 2024-10-16 04:20:55

您可以通过使用自定义扩展图形状态来实现此目的:

PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState();
graphicsState.setStrokingAlphaConstant(0.5f);
COSName graphicsStateName = page.getResources().add(graphicsState);
try (PDPageContentStream cs = new PDPageContentStream(document, page, true, true, true)) {
    cs.appendRawCommands("/" + graphicsStateName.getName() + " gs\n");
    // draw your line here.
}

You can achieve this by using a custom extended graphics state:

PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState();
graphicsState.setStrokingAlphaConstant(0.5f);
COSName graphicsStateName = page.getResources().add(graphicsState);
try (PDPageContentStream cs = new PDPageContentStream(document, page, true, true, true)) {
    cs.appendRawCommands("/" + graphicsStateName.getName() + " gs\n");
    // draw your line here.
}
无人问我粥可暖 2024-10-16 04:20:55

您不能使用 java.awt.Color 的 alpha 值,因为 PDFBox 仅使用 RGB 值。根据 public void setStrokingColor(Color color) 的 javadoc 它只是:

设置描边颜色,指定为
RGB。

一种选择是将背景颜色设置为描边颜色,以使线条不可见。
注意 - 隐形!=透明(这样你就不会获得透视效果)

You cannot use the alpha value of the java.awt.Color as PDFBox only uses the RGB value. As per javadoc of public void setStrokingColor(Color color) it just:

Set the stroking color, specified as
RGB.

One option could be that you set the background color as the stroking color to make your line invisible.
NOTE - Invisible != Transparent (so you won't get the see through effect)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文