I'm using iText 5.2.1 and I tried to use the BaseColor constructor with alpha channel, that is
public BaseColor(final int red, final int green, final int blue, final int alpha)
but when I actually draw text or shapes it seems that the alpha channel isn't taken into account. For example if I try this
Font f = ....;
f.setColor(new BaseColor(130, 130, 130, 50);
PdfContentByte cb = writer.getDirectContent();
ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("my text", f),
refPointX, refPointY, 0);
the text it's written with the color specified but without the alpha information, that is with the color with 100% opacity. The same thing happens if I try to draw some shape and I specify a fill color with transparency.
In the book iText in Action second edition there's nothing about transparency in colors.
Am I wrong?
I've found something in itext mailing list, I tried and... works! It's a undocumented feature. Anyway the following code does what I need:
PdfContentByte cb = writer.getDirectContent();
PdfGState gState = new PdfGState();
gState.setFillOpacity(0.1f);
cb.setGState(gState);
If a draw text or shapes, they have 10% opacity. With gState.setStrokeOpacity
I can also set opacity on strokes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With