Pergunta

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);  
Foi útil?

Solução

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)

Outras dicas

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

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.
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top