在 XPS 文件中处理图像 | Python

您将在这里找到代码,了解如何使用 Python 处理 XPS 文件中的图像。

在 XPS 文档中添加图像

Aspose.Page for Python via .NET 提供了 XpsPath 类,允许您将图像添加到 XPS 文档中。为此,您必须创建一个 Matrix 和一个 ImageBrush,然后保存 XpsDocument。以下代码片段演示了将图像添加到 XPS 文档的完整功能:

 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")

请参阅 .NETJavaC++ 中 XPS 文档中图像的处理方法。


运行此代码的结果如下:

添加图像

添加平铺图像

通过 .NET 开发的 Aspose.Page for Python 提供了 XpsPath 类,可让您将图像合并到 XPS 文档中。为此,您必须创建一个 Matrix 和一个 ImageBrush,然后将平铺模式设置为 set_tile_mode(XpsTileMode.Tile),最后保存 XpsDocument。以下代码片段演示了向 XPS 文档添加平铺图像的完整功能:

 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")

请参阅 .NETJavaC++ 中 XPS 文档中图像的处理方法。


运行此代码的结果如下:

添加平铺图像

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

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.