iTextsharp是否支持多色对角渐变? [英] Does iTextsharp support multi color diagonal gradients?
问题描述
我的目标是创建一个具有多种颜色的对角渐变作为椭圆的背景.可以使用以下代码完成水平或垂直渐变.我基本上用渐变阴影绘制矩形,并从中剪切出一个椭圆.
My goal is to create a diagonal gradient with multiple colors as the background of an ellipse. A horizontal or vertical gradient can be done with the code below. I basically draw rectangles with gradient shadings and clip an ellipse out of it.
这是我的水平渐变:
水平渐变的代码:
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();
有人知道是否有可能以显示对角线渐变的方式旋转渐变吗?感谢您的帮助!
Does anyone know if it's possible to rotate the gradient in a way that it will display a diagonal gradient? Thanks for any help!
推荐答案
我首先简化了您的代码...根本不需要绘制矩形:
I first simplified your code a bit... there is no need for drawing rectangles at all:
//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);
这也会导致
基于此代码,我稍微更改了阴影坐标:
Based on this code I changed the shading coordinates a bit:
//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);
结果是:
这应该是所需的对角梯度.
这篇关于iTextsharp是否支持多色对角渐变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!