Working with Gradient in XPS file | .NET
Add Gradient in XPS Document
Add Horizontal Gradient
Aspose.Page for .NET offers XpsGradientBrush class, with which you can add gradients on an XPS document.
You need to specify XpsGradientStop and add XpsPath to the object of XpsDocument class.
The following code snippet shows complete functionality to add horizontal gradient on the XPS document:
1// Paint rectangle with horizontal gradient fill in XPS document.
2
3// Create new XPS Document
4XpsDocument doc = new XpsDocument();
5
6string outputFileName = "HorizontalGradient_outXPS.xps";
7
8// Initialize List of XpsGradentStop
9List<XpsGradientStop> stops = new List<XpsGradientStop>();
10stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 244, 253, 225), 0.0673828f));
11stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 251, 240, 23), 0.314453f));
12stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 252, 209, 0), 0.482422f));
13stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 241, 254, 161), 0.634766f));
14stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 53, 253, 255), 0.915039f));
15stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 12, 91, 248), 1f));
16// Create new path by defining geometery in abbreviation form
17XpsPath path = doc.AddPath(doc.CreatePathGeometry("M 10,210 L 228,210 228,300 10,300"));
18path.RenderTransform = doc.CreateMatrix(1f, 0f, 0f, 1f, 20f, 70f);
19path.Fill = doc.CreateLinearGradientBrush(new PointF(10f, 0f), new PointF(228f, 0f));
20((XpsGradientBrush)path.Fill).GradientStops.AddRange(stops);
21// Save resultant XPS document
22doc.Save(OutputDir + outputFileName);
The result
Add Vertical Gradient
Aspose.Page for .NET offers XpsGradientBrush class, with which you can add gradients on an XPS document. You need to specify XpsGradientStop and add XpsPath to the object of XpsDocument class. The following code snippet shows complete functionality to add verticalal gradient on the XPS document:
1// Paint rectangle with vertical gradient fill in XPS document.
2
3// Create new XPS Document
4XpsDocument doc = new XpsDocument();
5
6string outputFileName = "VerticalGradient_outXPS.xps";
7
8// Initialize List of XpsGradentStop
9List<XpsGradientStop> stops = new List<XpsGradientStop>();
10stops.Add(doc.CreateGradientStop(doc.CreateColor(253, 255, 12, 0), 0f));
11stops.Add(doc.CreateGradientStop(doc.CreateColor(252, 255, 154, 0), 0.359375f));
12stops.Add(doc.CreateGradientStop(doc.CreateColor(252, 255, 56, 0), 0.424805f));
13stops.Add(doc.CreateGradientStop(doc.CreateColor(253, 255, 229, 0), 0.879883f));
14stops.Add(doc.CreateGradientStop(doc.CreateColor(252, 255, 255, 234), 1f));
15// Create new path by defining geometery in abbreviation form
16XpsPath path = doc.AddPath(doc.CreatePathGeometry("M 10,110 L 228,110 228,200 10,200"));
17path.RenderTransform = doc.CreateMatrix(1f, 0f, 0f, 1f, 20f, 70f);
18path.Fill = doc.CreateLinearGradientBrush(new PointF(10f, 110f), new PointF(10f, 200f));
19((XpsGradientBrush)path.Fill).GradientStops.AddRange(stops);
20// Save resultant XPS document
21doc.Save(OutputDir + outputFileName);
The result
Add Diagonal Gradient
Aspose.Page for .NET offers XpsGradientBrush class, with which you can add gradients on an XPS document. For this, first, specify XpsGradientStop and then add XpsPath to the object of XpsDocument class. The following code snippet shows complete functionality to add a diagonal gradient on the XPS document:
1// Paint rectangle with diagonal gradient fill in XPS document.
2
3// Create new XPS Document
4XpsDocument doc = new XpsDocument();
5
6string outputFileName = "DiagonalGradient_outXPS.xps";
7
8// Initialize List of XpsGradentStop
9List<XpsGradientStop> stops = new List<XpsGradientStop>();
10// Add Colors to Gradient
11stops.Add(doc.CreateGradientStop(doc.CreateColor(0, 142, 4), 0f));
12stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 202, 0), 0.144531f));
13stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 250, 0), 0.264648f));
14stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 0, 0), 0.414063f));
15stops.Add(doc.CreateGradientStop(doc.CreateColor(233, 0, 255), 0.544922f));
16stops.Add(doc.CreateGradientStop(doc.CreateColor(107, 27, 190), 0.694336f));
17stops.Add(doc.CreateGradientStop(doc.CreateColor(63, 0, 255), 0.844727f));
18stops.Add(doc.CreateGradientStop(doc.CreateColor(0, 199, 80), 1f));
19// Create new path by defining geometery in abbreviation form
20XpsPath path = doc.AddPath(doc.CreatePathGeometry("M 10,10 L 228,10 228,100 10,100"));
21path.RenderTransform = doc.CreateMatrix(1f, 0f, 0f, 1f, 20f, 70f);
22path.Fill = doc.CreateLinearGradientBrush(new PointF(10f, 10f), new PointF(228f, 100f));
23((XpsGradientBrush)path.Fill).GradientStops.AddRange(stops);
24// Save resultant XPS document
25doc.Save(OutputDir + outputFileName);
The result
You can download examples and data files from GitHub.