Working with Visual Brush in XPS file | Python
A visual brush is a powerful tool for creating visually compelling and dynamic designs in graphic design and software development contexts. They are used to fill or stroke shapes with visual content, such as images, gradients, or other graphical elements allowing you to paint one visual element with another one.
In more technical terms, a visual brush is a type of brush that references a visual object, such as an image or a UI element and uses that object to define the appearance of a shape or path. When applied to a shape, the visual brush renders the referenced visual object within the boundaries of the shape. They can be used to fill a shape with an image, create patterns or textures, or even apply complex graphical effects.
Add Grid using Visual Brush
Aspose.Page for Python via .NET provides the XpsVisualBrush Class, enabling you to add a grid to an XPS document. To achieve this, you must specify an XpsPathGeometry and add an XpsCanvas to the object of the XpsDocument class. The following code snippet demonstrates the complete functionality for adding a grid to an XPS document:
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_visual_brush()
11
12doc = XpsDocument()
13# Geometry for the magenta grid VisualBrush
14path_geometry = doc.create_path_geometry()
15path_geometry.add_segment(doc.create_poly_line_segment(
16[ aspose.pydrawing.PointF(240, 5), aspose.pydrawing.PointF(240, 310), aspose.pydrawing.PointF(0, 310) ], True))
17path_geometry[0].start_point = aspose.pydrawing.PointF(0, 5)
18
19# Canvas for the magenta grid VisualBrush
20visual_canvas = doc.create_canvas()
21
22visual_path = visual_canvas.add_path(
23doc.create_path_geometry("M 0,4 L 4,4 4,0 6,0 6,4 10,4 10,6 6,6 6,10 4,10 4,6 0,6 Z"))
24visual_path.fill = doc.create_solid_color_brush(doc.create_color(1, .61, 0.1, 0.61))
25
26grid_path = doc.create_path(path_geometry)
27#Create a Visual Brush, it is specified by some XPS fragment (vector graphics and glyphs)
28visualBrush: XpsVisualBrush = doc.create_visual_brush(visual_canvas,
29 aspose.pydrawing.RectangleF(0, 0, 10, 10), aspose.pydrawing.RectangleF(0, 0, 10, 10))
30grid_path.fill = visualBrush
31visualBrush.tile_mode = XpsTileMode.TILE
32# New canvas
33canvas = doc.add_canvas()
34canvas.render_transform = doc.create_matrix(1, 0, 0, 1, 268, 70)
35# Add a grid
36canvas.add_path(path_geometry)
37# Red transparent rectangle in the middle top
38path = canvas.add_path(doc.create_path_geometry("M 30,20 l 258.24,0 0,56.64 -258.24,0 Z"))
39path = canvas.add_path(doc.create_path_geometry("M 10,10 L 228,10 228,100 10,100"))
40path.fill = doc.create_solid_color_brush(doc.create_color(1.0, 0.0, 0.0))
41path.opacity = 0.7
42# Save the resultant XPS document
43doc.save(data_dir + "AddGrid_out.xps")
The result is next
You can download examples and data files from GitHub.