iText7 C# 无法设置SquareAnnotation方形注释内部颜色透明度的问题
解决方案
你当前的代码存在核心问题导致透明度不生效,修正方案如下:
- 透明度参数范围错误:iText 7中所有Opacity相关方法的入参是
0~1区间的浮点数,0代表完全透明,1代表完全不透明。你传入的25、30远大于1,等价于设置完全不透明,因此看不到透明度效果。 SetOpacity()是设置注释整体的透明度,若只需控制内部填充颜色的透明度,单独使用SetNonStrokingOpacity()即可。- 若修正参数后依旧不生效,可升级iText7到最新稳定版,部分旧版本存在注释透明度属性不生效的已知问题。
修正后的代码:
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.."); // 填充透明度设置为0.25(即25%不透明度,75%透明),根据需求调整0~1之间的数值即可 squareAnnotation.SetNonStrokingOpacity(0.25f); // 如果需要边框也同步设置透明度,取消下面的注释即可 // squareAnnotation.SetStrokingOpacity(0.25f); squareAnnotation.SetInteriorColor(new float[] { (float)0.294, (float)0.552, (float)0.968 }); return squareAnnotation; }
修改后重新运行即可看到方形注释内部颜色的透明度效果。
内容的提问来源于stack exchange,提问作者Dangerous Dev
相关产品推荐
相关产品推荐

