Does iTextsharp support multi color diagonal gradients?

时光总嘲笑我的痴心妄想 提交于 2020-01-06 11:09:20

问题


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.

Here's my horizontal gradient:

The code for a horizontal gradient:

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!


回答1:


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

This also results in

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

The result is:

This should be the desired diagonal gradient.



来源:https://stackoverflow.com/questions/29282164/does-itextsharp-support-multi-color-diagonal-gradients

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!