Does iTextsharp support multi-color diagonal gradients?

My goal is to create a diagonal gradient with several colors as an ellipse background. Horizontal or vertical gradient can be done using the code below. I basically draw rectangles with gradient shades and cut an ellipse from it.

Here is my horizontal gradient:

gradient

Horizontal Gradient Code:

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 the gradient can be rotated so that it displays a diagonal gradient? Thanks for any help!

0
source share
1 answer

At first I simplified your code a bit ... there is no need to draw rectangles:

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

It also leads to

Simplified gradient

:

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

:

Diagonal gradient

.

+2

Source: https://habr.com/ru/post/1621420/


All Articles