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// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
2// The path to the documents directory.
3string dataDir = RunExamples.GetDataDir_WorkingWithGradient();
4// Create new XPS Document
5XpsDocument doc = new XpsDocument();
6// Initialize List of XpsGradentStop
7List<XpsGradientStop> stops = new List<XpsGradientStop>();
8stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 244, 253, 225), 0.0673828f));
9stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 251, 240, 23), 0.314453f));
10stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 252, 209, 0), 0.482422f));
11stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 241, 254, 161), 0.634766f));
12stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 53, 253, 255), 0.915039f));
13stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 12, 91, 248), 1f));
14// Create new path by defining geometery in abbreviation form
15XpsPath path = doc.AddPath(doc.CreatePathGeometry("M 10,210 L 228,210 228,300 10,300"));
16path.RenderTransform = doc.CreateMatrix(1f, 0f, 0f, 1f, 20f, 70f);
17path.Fill = doc.CreateLinearGradientBrush(new PointF(10f, 0f), new PointF(228f, 0f));
18((XpsGradientBrush)path.Fill).GradientStops.AddRange(stops);
19// Save resultant XPS document
20doc.Save(dataDir + "AddHorizontalGradient_out.xps");
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// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
2// The path to the documents directory.
3string dataDir = RunExamples.GetDataDir_WorkingWithGradient();
4// Create new XPS Document
5XpsDocument doc = new XpsDocument();
6// Initialize List of XpsGradentStop
7List<XpsGradientStop> stops = new List<XpsGradientStop>();
8stops.Add(doc.CreateGradientStop(doc.CreateColor(253, 255, 12, 0), 0f));
9stops.Add(doc.CreateGradientStop(doc.CreateColor(252, 255, 154, 0), 0.359375f));
10stops.Add(doc.CreateGradientStop(doc.CreateColor(252, 255, 56, 0), 0.424805f));
11stops.Add(doc.CreateGradientStop(doc.CreateColor(253, 255, 229, 0), 0.879883f));
12stops.Add(doc.CreateGradientStop(doc.CreateColor(252, 255, 255, 234), 1f));
13// Create new path by defining geometery in abbreviation form
14XpsPath path = doc.AddPath(doc.CreatePathGeometry("M 10,110 L 228,110 228,200 10,200"));
15path.RenderTransform = doc.CreateMatrix(1f, 0f, 0f, 1f, 20f, 70f);
16path.Fill = doc.CreateLinearGradientBrush(new PointF(10f, 110f), new PointF(10f, 200f));
17((XpsGradientBrush)path.Fill).GradientStops.AddRange(stops);
18// Save resultant XPS document
19doc.Save(dataDir + "AddVerticalGradient_out.xps");
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// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
2// The path to the documents directory.
3string dataDir = RunExamples.GetDataDir_WorkingWithGradient();
4// Create new XPS Document
5XpsDocument doc = new XpsDocument();
6// Initialize List of XpsGradentStop
7List<XpsGradientStop> stops = new List<XpsGradientStop>();
8// Add Colors to Gradient
9stops.Add(doc.CreateGradientStop(doc.CreateColor(0, 142, 4), 0f));
10stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 202, 0), 0.144531f));
11stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 250, 0), 0.264648f));
12stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 0, 0), 0.414063f));
13stops.Add(doc.CreateGradientStop(doc.CreateColor(233, 0, 255), 0.544922f));
14stops.Add(doc.CreateGradientStop(doc.CreateColor(107, 27, 190), 0.694336f));
15stops.Add(doc.CreateGradientStop(doc.CreateColor(63, 0, 255), 0.844727f));
16stops.Add(doc.CreateGradientStop(doc.CreateColor(0, 199, 80), 1f));
17// Create new path by defining geometery in abbreviation form
18XpsPath path = doc.AddPath(doc.CreatePathGeometry("M 10,10 L 228,10 228,100 10,100"));
19path.RenderTransform = doc.CreateMatrix(1f, 0f, 0f, 1f, 20f, 70f);
20path.Fill = doc.CreateLinearGradientBrush(new PointF(10f, 10f), new PointF(228f, 100f));
21((XpsGradientBrush)path.Fill).GradientStops.AddRange(stops);
22// Save resultant XPS document
23doc.Save(dataDir + "AddLinearGradient_out.xps");
The result
You can download examples and data files from GitHub.