Working with Gradient in XPS file | Python

A gradient is a gradual transition between two or more colors or shades. In visual arts, graphics, and design, gradients are often used to create smooth transitions from one color to another, adding depth, dimension, and visual interest to an object or image. Gradients can vary in complexity, from simple two-color gradients to more intricate blends involving multiple colors or even transparency levels.

Here you will find out how to add different types of gradients to XPS files in Python.

Add Gradient in XPS Document

Add Horizontal Gradient

Aspose.Page for Python via .NET provides the XpsGradientBrush Class, created to incorporate gradients into XPS documents. To achieve this, you must specify XpsGradientStop and add XpsPath to the object of the XpsDocument class. The following code snippet demonstrates the complete functionality for adding a horizontal gradient to an XPS document:

See how to work with a gradient in XPS documents via .NET, Java and C++.


The result

Add Horizontal Gradient

 1from aspose.page.xps import *
 2from aspose.page.xps.xpsmodel import *
 3import aspose.pydrawing
 4from util import Util
 5###############################################
 6###### Class and Method declaration here ######
 7###############################################
 8
 9# The path to the documents directory.
10data_dir = Util.get_data_dir_working_with_gradient()
11# Create a new XPS Document
12doc = XpsDocument()
13# Initialize a List of XpsGradentStop
14stops = []
15stops.append(doc.create_gradient_stop(doc.create_color(255, 244, 253, 225), 0.0673828))
16stops.append(doc.create_gradient_stop(doc.create_color(255, 251, 240, 23), 0.314453))
17stops.append(doc.create_gradient_stop(doc.create_color(255, 252, 209, 0), 0.482422))
18stops.append(doc.create_gradient_stop(doc.create_color(255, 241, 254, 161), 0.634766))
19stops.append(doc.create_gradient_stop(doc.create_color(255, 53, 253, 255), 0.915039))
20stops.append(doc.create_gradient_stop(doc.create_color(255, 12, 91, 248), 1))
21# Create a new path by defining geometery in an abbreviation form
22path = doc.add_path(doc.create_path_geometry("M 10,210 L 228,210 228,300 10,300"))
23path.render_transform = doc.create_matrix(1, 0, 0, 1, 20, 70)
24gradient: XpsLinearGradientBrush = doc.create_linear_gradient_brush(aspose.pydrawing.PointF(10, 0), aspose.pydrawing.PointF(228, 0))
25path.fill = gradient
26gradient.gradient_stops.extend(stops)
27# Save the resultant XPS document
28doc.save(data_dir + "AddHorizontalGradient_outXPS.xps")

Add Vertical Gradient

Aspose.Page for Python via .NET includes the XpsGradientBrush Class, for incorporating gradients into XPS documents. To accomplish this, you must specify XpsGradientStop and add XpsPath to the object of XpsDocument class. The following code snippet demonstrates the complete functionality for adding a vertical gradient to an XPS document:

See how to work with a gradient in XPS documents via .NET, Java and C++.


The result

Add Vertical Gradient

 1from aspose.page.xps import *
 2from aspose.page.xps.xpsmodel import *
 3import aspose.pydrawing
 4from util import Util
 5###############################################
 6###### Class and Method declaration here ######
 7###############################################
 8
 9# The path to the documents directory.
10data_dir = Util.get_data_dir_working_with_gradient()
11# Create a new XPS Document
12doc = XpsDocument()
13# Initialize a List of XpsGradentStop
14stops = []
15stops.append(doc.create_gradient_stop(doc.create_color(253, 255, 12, 0), 0))
16stops.append(doc.create_gradient_stop(doc.create_color(252, 255, 154, 0), 0.359375))
17stops.append(doc.create_gradient_stop(doc.create_color(252, 255, 56, 0), 0.424805))
18stops.append(doc.create_gradient_stop(doc.create_color(253, 255, 229, 0), 0.879883))
19stops.append(doc.create_gradient_stop(doc.create_color(252, 255, 255, 234), 1))
20# Create a new path by defining geometery in an abbreviation form
21path = doc.add_path(doc.create_path_geometry("M 10,110 L 228,110 228,200 10,200"))
22path.render_transform = doc.create_matrix(1, 0, 0, 1, 20, 70)
23gradient: XpsLinearGradientBrush = doc.create_linear_gradient_brush(aspose.pydrawing.PointF(10, 110), aspose.pydrawing.PointF(10, 200))
24path.fill = gradient
25gradient.gradient_stops.extend(stops)
26# Save the resultant XPS document
27doc.save(data_dir + "AddVerticalGradient_outXPS.xps")

Add Diagonal Gradient

The next code snippet shows complete functionality of how to add a linear gradient on an XPS document:

 1# The path to the documents directory.
 2data_dir = Util.get_data_dir_working_with_gradient()
 3# Create a new XPS Document
 4doc = XpsDocument()
 5# Initialize a List of XpsGradentStop
 6stops = []
 7# Add Colors to Gradient
 8stops.append(doc.create_gradient_stop(doc.create_color(0, 142, 4), 0))
 9stops.append(doc.create_gradient_stop(doc.create_color(255, 202, 0), 0.144531))
10stops.append(doc.create_gradient_stop(doc.create_color(255, 250, 0), 0.264648))
11stops.append(doc.create_gradient_stop(doc.create_color(255, 0, 0), 0.414063))
12stops.append(doc.create_gradient_stop(doc.create_color(233, 0, 255), 0.544922))
13stops.append(doc.create_gradient_stop(doc.create_color(107, 27, 190), 0.694336))
14stops.append(doc.create_gradient_stop(doc.create_color(63, 0, 255), 0.844727))
15stops.append(doc.create_gradient_stop(doc.create_color(0, 199, 80), 1))
16# Create a new path by defining geometery in an abbreviation form
17path = doc.add_path(doc.create_path_geometry("M 10,10 L 228,10 228,100 10,100"))
18path.render_transform = doc.create_matrix(1, 0, 0, 1, 20, 70)
19gradient: XpsLinearGradientBrush = doc.create_linear_gradient_brush(aspose.pydrawing.PointF(10, 10), aspose.pydrawing.PointF(228, 100))
20path.fill = gradient
21gradient.gradient_stops.extend(stops)
22# Save the resultant XPS document
23doc.save(data_dir + "AddDiagonalGradient_outXPS.xps")

See how to work with a gradient in XPS documents via .NET, Java and C++.


The result

Add Diagonal Gradient

You can download examples and data files from GitHub.

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.