Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does iText honors color transparency?

Tags:

itext

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?

like image 203
Pier Luigi Avatar asked Jul 03 '12 13:07

Pier Luigi


1 Answers

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.

like image 52
Pier Luigi Avatar answered Nov 14 '22 02:11

Pier Luigi