用PDFBox绘制透明线条

时间:2010-12-27 17:41:33

标签: java pdfbox

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

3 个答案:

答案 0 :(得分:3)

自PDFBox 2.0 appendRawCommands起,不推荐使用。

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

答案 1 :(得分:2)

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

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.
}

答案 2 :(得分:0)

您不能使用java.awt.Color的alpha值,因为PDFBox仅使用RGB值。根据{{​​1}}的javadoc,它只是:

  

设置描边的描边颜色   RGB。

一个选项可能是您将背景颜色设置为抚摸颜色,以使您的线条不可见。 注意 - 不可见!=透明(因此您无法获得透视效果)

相关问题