7

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?

4

1 回答 1

16

我在 itext 邮件列表中找到了一些东西,我尝试过并且......有效!这是一个未记录的功能。无论如何,以下代码可以满足我的需要:

PdfContentByte cb = writer.getDirectContent();
PdfGState gState = new PdfGState();
gState.setFillOpacity(0.1f);
cb.setGState(gState);

如果绘制文本或形状,它们有 10% 的不透明度。我gState.setStrokeOpacity还可以设置笔画的不透明度。

于 2012-07-04T07:37:53.130 回答