我的目标是创建一个多个颜色的对角线渐变作为椭圆的背景。可以使用下面的代码完成水平或垂直渐变。我基本上用渐变阴影绘制矩形并从中剪出椭圆。
这是我的水平渐变:
水平渐变的代码:
var cb = writer.DirectContent;
var boundingBox = new Rectangle(200, 200, 700, 350);
//draw a path, for example an ellipse
cb.Ellipse(boundingBox.Left, boundingBox.Bottom, boundingBox.Right, boundingBox.Top);
cb.Clip();//set clipping mask
cb.NewPath();
//gradient 1: yellow to gray
var gradientRect1 = new Rectangle(200, 200, 400, 350);
PdfShading shading = PdfShading.SimpleAxial(writer, gradientRect1.Left, gradientRect1.Bottom + (gradientRect1.Height / 2), gradientRect1.Right, gradientRect1.Bottom + (gradientRect1.Height / 2), BaseColor.YELLOW, BaseColor.DARK_GRAY);
var pattern = new PdfShadingPattern(shading);
gradientRect1.BackgroundColor = new ShadingColor(pattern);
cb.Rectangle(gradientRect1);
cb.Fill();
//gradient 2, gray to red
var gradientRect2 = new Rectangle(400, 200, 700, 350);
PdfShading shading2 = PdfShading.SimpleAxial(writer, gradientRect2.Left, gradientRect2.Bottom + (gradientRect2.Height / 2), gradientRect2.Right, gradientRect2.Bottom + (gradientRect2.Height / 2), BaseColor.DARK_GRAY, BaseColor.RED);
var pattern2 = new PdfShadingPattern(shading2);
gradientRect2.BackgroundColor = new ShadingColor(pattern2);
cb.Rectangle(gradientRect2);
cb.Fill();
是否有人知道是否可以以显示对角线渐变的方式旋转渐变?谢谢你的帮助!
答案 0 :(得分:2)
我首先简化了你的代码......根本不需要绘制矩形:
//draw a path, for example an ellipse
cb.Ellipse(boundingBox.Left, boundingBox.Bottom, boundingBox.Right, boundingBox.Top);
cb.Clip();//set clipping mask
cb.NewPath();
//gradient 1: yellow to gray
PdfShading shading = PdfShading.SimpleAxial(writer, 200, 275, 400, 275, BaseColor.YELLOW, BaseColor.DARK_GRAY, true, false);
cb.PaintShading(shading);
//gradient 2, gray to red
PdfShading shading2 = PdfShading.SimpleAxial(writer, 400, 275, 700, 275, BaseColor.DARK_GRAY, BaseColor.RED, false, true);
cb.PaintShading(shading2);
这也导致了
根据这段代码,我稍微更改了阴影坐标:
//gradient 1: yellow to gray
PdfShading shading = PdfShading.SimpleAxial(writer, 300, 175, 400, 275, BaseColor.YELLOW, BaseColor.DARK_GRAY, true, false);
cb.PaintShading(shading);
//gradient 2, gray to red
PdfShading shading2 = PdfShading.SimpleAxial(writer, 400, 275, 600, 475, BaseColor.DARK_GRAY, BaseColor.RED, false, true);
cb.PaintShading(shading2);
结果是:
这应该是所需的对角线渐变。