【问题标题】:Does iTextsharp support multi color diagonal gradients?iTextsharp 是否支持多色对角渐变?
【发布时间】:2015-03-26 15:14:14
【问题描述】:

我的目标是创建一个具有多种颜色的对角渐变作为椭圆的背景。可以使用下面的代码完成水平或垂直渐变。我基本上用渐变阴影绘制矩形并从中剪出一个椭圆。

这是我的水平渐变:

水平渐变的代码:

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();

有谁知道是否可以旋转渐变以显示对角渐变?感谢您的帮助!

【问题讨论】:

  • 您使用的着色轴坐标表示水平轴。您是否尝试过使用非水平轴?
  • 是的,可以指定对角坐标,但这仅适用于一种颜色过渡。

标签: c# itextsharp linear-gradients


【解决方案1】:

我首先简化了你的代码......根本不需要绘制矩形:

//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);

结果是:

这应该是所需的对角渐变

【讨论】:

  • 非常感谢!我认为矩形(或任何其他路径)是必需的。
  • PDF 有一个 Shading Operator sh绘制阴影字典描述的形状和颜色阴影,以当前剪切路径为准 这里使用的;
猜你喜欢
  • 2011-04-02
  • 2012-02-14
  • 1970-01-01
  • 2011-06-25
  • 1970-01-01
  • 2017-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多