Search code examples
c#itext.net-5itext7

iText7 - unable to set the the Opacity for SquareAnnotation Interior color


Team, I am working with iText7 C# library. I am not able to set the Opacity for the Square Annotation InteriorColor.

Below is the code I am using. I tried with all the different methods we have for a SquareAnnotation to set the Opacity but, no luck. am I missing anything?

private PdfSquareAnnotation AddAnnotation(float rectHeight, float rectWidth, float x, float y)
    {
        Rectangle rect = new Rectangle(x, y, rectWidth, rectHeight);
        PdfSquareAnnotation squareAnnotation = new PdfSquareAnnotation(rect);
        squareAnnotation.SetColor(ColorConstants.GREEN);
        squareAnnotation.SetTitle(new PdfString("This is the title"));
        squareAnnotation.SetContents("This is the contents of the annotation. bla bla..");
        squareAnnotation.SetNonStrokingOpacity(25);
        squareAnnotation.SetOpacity(new PdfNumber(30));
        squareAnnotation.SetInteriorColor(new float[] { (float)0.294, (float)0.552, (float)0.968 });

        //squareAnnotation.SetStrokingOpacity(25);


        return squareAnnotation;
    }

Output:

Opacity for interior color is not being applied

enter image description here

Thank you in advance


Solution

  • The cause is that you use opacity values 25 and 30 - the opacity value range is 0.0 (fully transparent) to 1.0 (fully opaque). Thus, 25 and 30 effectively are excessively opaque.

    By switching to

    squareAnnotation.SetNonStrokingOpacity(0.25f);
    squareAnnotation.SetOpacity(new PdfNumber(0.30f));
    

    you get the desired partial transparency.