Working with Images in XPS file | Python
Here you will find the code that explains to you how to work with images in XPS files using Python.
Add Images inside XPS Documents
Aspose.Page for Python via .NET provides the XpsPath Class, allowing you to incorporate images into XPS documents. To achieve this, you must create a Matrix and an ImageBrush, and then save the XpsDocument. The following code snippet demonstrates the complete functionality for adding an image to an XPS document:
1# The path to the documents directory.
2data_dir = Util.get_data_dir_working_with_images()
3# Create a new XPS Document
4doc = XpsDocument()
5# Add an Image
6path = doc.add_path(doc.create_path_geometry("M 30,20 l 258.24,0 0,56.64 -258.24,0 Z"))
7# Creating a matrix is optional, it can be used for the proper positioning
8path.render_transform = doc.create_matrix(0.7, 0, 0, 0.7, 0, 20)
9# Create an Image Brush
10path.fill = doc.create_image_brush(data_dir + "QL_logo_color.tif", aspose.pydrawing.RectangleF(0, 0, 258.24, 56.64),
11aspose.pydrawing.RectangleF(50, 20, 193.68, 42.48))
12# Save the resultant XPS document
13doc.save(data_dir + "AddImage_outXPS.xps")
The result of running this code is next
Add Tiled Images
Aspose.Page for Python via .NET provides the XpsPath Class, enabling you to incorporate images into XPS documents. To achieve this, you must create a Matrix and an ImageBrush, then set the tile mode to set_tile_mode(XpsTileMode.Tile), and finally save the XpsDocument. The following code snippet demonstrates the complete functionality for adding a tiled image to an XPS document:
1# The path to the documents directory.
2data_dir = Util.get_data_dir_working_with_images()
3# Create a new XPS Document
4doc = XpsDocument()
5# Tile an image
6# ImageBrush filled rectangle in the right top bellow
7path = doc.add_path(doc.create_path_geometry("M 10,160 L 228,160 228,305 10,305"))
8imageBrush: XpsImageBrush = doc.create_image_brush(data_dir + "R08LN_NN.jpg", aspose.pydrawing.RectangleF(0, 0, 128, 96),
9 aspose.pydrawing.RectangleF(0, 0, 64, 48))
10path.fill = imageBrush
11imageBrush.tile_mode = XpsTileMode.TILE
12path.fill.opacity = 0.5
13# Save the resultant XPS document
14doc.save(data_dir + "AddTiledImage_outXPS.xps")
The result of running this code is next
You can download examples and data files from GitHub.