在 XPS 文件中使用渐变 | Python
Contents
[
Hide
Show
]渐变是指两种或多种颜色或色调之间的逐渐过渡。在视觉艺术、图形和设计领域,渐变通常用于创建从一种颜色到另一种颜色的平滑过渡,从而为对象或图像增添深度、维度和视觉趣味。渐变的复杂程度各不相同,从简单的双色渐变到涉及多种颜色甚至透明度的更复杂的混合。
在这里,您将了解如何在 Python 中向 XPS 文件添加不同类型的渐变。
在 XPS 文档中添加渐变
添加水平渐变
Aspose.Page for Python via .NET 提供了 XpsGradientBrush 类,用于将渐变合并到 XPS 文档中。为此,您必须指定 XpsGradientStop 并将 XpsPath 添加到 XpsDocument 类的对象中。以下代码片段演示了向 XPS 文档添加水平渐变的完整功能:
结果
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")
添加垂直渐变
Aspose.Page for Python via .NET 包含 XpsGradientBrush 类,用于将渐变效果添加到 XPS 文档中。为此,您必须指定 XpsGradientStop 并将 XpsPath 添加到 XpsDocument 类的对象中。以下代码片段演示了向 XPS 文档添加垂直渐变的完整功能:
结果
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")
添加对角渐变
以下代码片段展示了如何在 XPS 文档中添加线性渐变的完整功能:
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")
结果
您可以从 GitHub下载示例和数据文件。