2

我想在 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);  
4

3 回答 3

5

自 PDFBox 2.0 起appendRawCommands已弃用。

    float alpha = 0.5f;
    PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState();
    graphicsState.setStrokingAlphaConstant(alpha);
    stream.setGraphicsStateParameters(graphicsState);
    // draw line here
于 2017-05-18T17:53:32.197 回答
3

您可以通过使用自定义扩展图形状态来实现这一点:

PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState();
graphicsState.setStrokingAlphaConstant(0.5f);
COSName graphicsStateName = page.getResources().add(graphicsState);
尝试 (PDPageContentStream cs = new PDPageContentStream(document, page, true, true, true)) {
    cs.appendRawCommands("/" + graphicsStateName.getName() + " gs\n");
    // 在这里画线。
}
于 2015-04-20T10:21:36.353 回答
-1

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

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

一种选择可能是将背景颜色设置为描边颜色以使线条不可见。 注意 -不可见!= 透明(所以你不会得到透视效果)

于 2010-12-31T05:19:20.860 回答