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:

Voir comment travailler avec un dégradé dans les documents XPS via .NET, Java and C++.


The result

Add Horizontal Gradient

 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 = []
 7stops.append(doc.create_gradient_stop(doc.create_color(255, 244, 253, 225), 0.0673828))
 8stops.append(doc.create_gradient_stop(doc.create_color(255, 251, 240, 23), 0.314453))
 9stops.append(doc.create_gradient_stop(doc.create_color(255, 252, 209, 0), 0.482422))
10stops.append(doc.create_gradient_stop(doc.create_color(255, 241, 254, 161), 0.634766))
11stops.append(doc.create_gradient_stop(doc.create_color(255, 53, 253, 255), 0.915039))
12stops.append(doc.create_gradient_stop(doc.create_color(255, 12, 91, 248), 1))
13# Create a new path by defining geometery in an abbreviation form
14path = doc.add_path(doc.create_path_geometry("M 10,210 L 228,210 228,300 10,300"))
15path.render_transform = doc.create_matrix(1, 0, 0, 1, 20, 70)
16gradient: XpsLinearGradientBrush = doc.create_linear_gradient_brush(aspose.pydrawing.PointF(10, 0), aspose.pydrawing.PointF(228, 0))
17path.fill = gradient
18gradient.gradient_stops.extend(stops)
19# Save the resultant XPS document
20doc.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:

Voir comment travailler avec un dégradé dans les documents XPS via .NET, Java and C++.


The result

Add Vertical Gradient

 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 = []
 7stops.append(doc.create_gradient_stop(doc.create_color(253, 255, 12, 0), 0))
 8stops.append(doc.create_gradient_stop(doc.create_color(252, 255, 154, 0), 0.359375))
 9stops.append(doc.create_gradient_stop(doc.create_color(252, 255, 56, 0), 0.424805))
10stops.append(doc.create_gradient_stop(doc.create_color(253, 255, 229, 0), 0.879883))
11stops.append(doc.create_gradient_stop(doc.create_color(252, 255, 255, 234), 1))
12# Create a new path by defining geometery in an abbreviation form
13path = doc.add_path(doc.create_path_geometry("M 10,110 L 228,110 228,200 10,200"))
14path.render_transform = doc.create_matrix(1, 0, 0, 1, 20, 70)
15gradient: XpsLinearGradientBrush = doc.create_linear_gradient_brush(aspose.pydrawing.PointF(10, 110), aspose.pydrawing.PointF(10, 200))
16path.fill = gradient
17gradient.gradient_stops.extend(stops)
18# Save the resultant XPS document
19doc.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")

Voir comment travailler avec un dégradé dans les documents XPS 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.