Working with Visual Brush in XPS file | Python

Contents
[ Hide Show ]

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:

 1# The path to the documents directory.
 2data_dir = Util.get_data_dir_working_with_visual_brush()
 3
 4doc = XpsDocument()
 5# Geometry for the magenta grid VisualBrush
 6path_geometry = doc.create_path_geometry()
 7path_geometry.add_segment(doc.create_poly_line_segment(
 8[ aspose.pydrawing.PointF(240, 5), aspose.pydrawing.PointF(240, 310), aspose.pydrawing.PointF(0, 310) ], True))
 9path_geometry[0].start_point = aspose.pydrawing.PointF(0, 5)
10
11# Canvas for the magenta grid VisualBrush
12visual_canvas = doc.create_canvas()
13
14visual_path = visual_canvas.add_path(
15doc.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"))
16visual_path.fill = doc.create_solid_color_brush(doc.create_color(1, .61, 0.1, 0.61))
17
18grid_path = doc.create_path(path_geometry)
19#Create a Visual Brush, it is specified by some XPS fragment (vector graphics and glyphs)
20visualBrush: XpsVisualBrush = doc.create_visual_brush(visual_canvas,
21    aspose.pydrawing.RectangleF(0, 0, 10, 10), aspose.pydrawing.RectangleF(0, 0, 10, 10))
22grid_path.fill = visualBrush
23visualBrush.tile_mode = XpsTileMode.TILE
24# New canvas
25canvas = doc.add_canvas()
26canvas.render_transform = doc.create_matrix(1, 0, 0, 1, 268, 70)
27# Add a grid
28canvas.add_path(path_geometry)
29# Red transparent rectangle in the middle top
30path = canvas.add_path(doc.create_path_geometry("M 30,20 l 258.24,0 0,56.64 -258.24,0 Z"))
31path = canvas.add_path(doc.create_path_geometry("M 10,10 L 228,10 228,100 10,100"))
32path.fill = doc.create_solid_color_brush(doc.create_color(1.0, 0.0, 0.0))
33path.opacity = 0.7
34# Save the resultant XPS document
35doc.save(data_dir + "AddGrid_out.xps")

See working with visual brush in XPS documents in .NET, Java and C++.


The result is next

Add Grid

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.