在 XPS 文件中处理图像 | Python
Contents
[
Hide
Show
]您将在这里找到代码,了解如何使用 Python 处理 XPS 文件中的图像。
在 XPS 文档中添加图像
Aspose.Page for Python via .NET 提供了 XpsPath 类,允许您将图像添加到 XPS 文档中。为此,您必须创建一个 Matrix 和一个 ImageBrush,然后保存 XpsDocument。以下代码片段演示了将图像添加到 XPS 文档的完整功能:
1from aspose.page.xps import *
2from aspose.page.xps.xpsmodel import *
3import aspose.pydrawing
4from util import Util
5# The path to the documents directory.
6data_dir = Util.get_data_dir_working_with_images()
7# Create a new XPS Document
8doc = XpsDocument()
9# Add an Image
10path = doc.add_path(doc.create_path_geometry("M 30,20 l 258.24,0 0,56.64 -258.24,0 Z"))
11# Creating a matrix is optional, it can be used for the proper positioning
12path.render_transform = doc.create_matrix(0.7, 0, 0, 0.7, 0, 20)
13# Create an Image Brush
14path.fill = doc.create_image_brush(data_dir + "QL_logo_color.tif", aspose.pydrawing.RectangleF(0, 0, 258.24, 56.64),
15aspose.pydrawing.RectangleF(50, 20, 193.68, 42.48))
16# Save the resultant XPS document
17doc.save(data_dir + "AddImage_outXPS.xps")运行此代码的结果如下:

添加平铺图像
通过 .NET 开发的 Aspose.Page for Python 提供了 XpsPath 类,可让您将图像合并到 XPS 文档中。为此,您必须创建一个 Matrix 和一个 ImageBrush,然后将平铺模式设置为 set_tile_mode(XpsTileMode.Tile),最后保存 XpsDocument。以下代码片段演示了向 XPS 文档添加平铺图像的完整功能:
1from aspose.page.xps import *
2from aspose.page.xps.xpsmodel import *
3import aspose.pydrawing
4from util import Util
5# The path to the documents directory.
6data_dir = Util.get_data_dir_working_with_images()
7# Create a new XPS Document
8doc = XpsDocument()
9# Tile an image
10# ImageBrush filled rectangle in the right top bellow
11path = doc.add_path(doc.create_path_geometry("M 10,160 L 228,160 228,305 10,305"))
12imageBrush: XpsImageBrush = doc.create_image_brush(data_dir + "R08LN_NN.jpg", aspose.pydrawing.RectangleF(0, 0, 128, 96),
13 aspose.pydrawing.RectangleF(0, 0, 64, 48))
14path.fill = imageBrush
15imageBrush.tile_mode = XpsTileMode.TILE
16path.fill.opacity = 0.5
17# Save the resultant XPS document
18doc.save(data_dir + "AddTiledImage_outXPS.xps")运行此代码的结果如下:

您可以从 GitHub下载示例和数据文件。