在 XPS 文件中使用透明度 | Python

透明度通常用于创建视觉效果,例如混合、淡入淡出或将一个图像或图形元素叠加到另一个图像或图形元素上。它可以通过多种技术实现,包括调整对象的不透明度或 Alpha 通道、使用混合模式、应用蒙版或叠加层。透明度是设计、插画和数字合成中的重要工具,可用于创建层次丰富、复杂的视觉构图。

将透明对象添加到 XPS 文档

Aspose.Page for Python via .NET 提供了 XpsPath 类,允许您将透明对象添加到 XPS 文档。为此,您必须指定一个 PathGeometry 并将其添加到 XpsPath。以下代码片段演示了将透明对象添加到 XPS 文档的完整功能:

 1# The path to the documents directory.
 2        data_dir = Util.get_data_dir_working_with_transparency()
 3        # Create new XPS Document
 4        doc = XpsDocument()
 5        
 6        # Just to demonstrate transparency
 7        doc.add_path(doc.create_path_geometry("M120,0 H400 v1000 H120")).fill = doc.create_solid_color_brush(aspose.pydrawing.Color.gray)
 8        doc.add_path(doc.create_path_geometry("M300,120 h600 V420 h-600")).fill = doc.create_solid_color_brush(aspose.pydrawing.Color.gray)
 9        
10        # Create the path with the closed rectangle geometry
11        path1: XpsPath = doc.create_path(doc.create_path_geometry("M20,20 h200 v200 h-200 z"))
12        # Set a blue solid brush to fill the path1
13        path1.fill = doc.create_solid_color_brush(aspose.pydrawing.Color.blue)
14        # Add it to the current page
15        path2: XpsPath = doc.add_path(path1)
16        
17        # path1 and path2 are the same as soon as path1 hasn't been placed inside any other element
18        # (which means that path1 had no parent element).
19        # Because of that rectangle's color on the page effectively turns to green
20        path2.fill = doc.create_solid_color_brush(aspose.pydrawing.Color.green)
21        
22        # Now add path2 once again. Now path2 has parent. So path3 won't be the same as path2.
23        # Thus a new rectangle is painted on the page ...
24        path3: XpsPath = doc.add_path(path2)
25        # ... and we shift it 300 units lower ...
26        path3.render_transform = doc.create_matrix(1, 0, 0, 1, 0, 300)
27        # ... and set red solid brush to fill it
28        path3.fill = doc.create_solid_color_brush(aspose.pydrawing.Color.red)
29        
30        # Create a new path4 with path2's geometry ...
31        path4: XpsPath = doc.add_path(path2.data)
32        # ... shift it 300 units to the right ...
33        path4.render_transform = doc.create_matrix(1, 0, 0, 1, 300, 0)
34        # ... and set a blue solid fill
35        path4.fill = doc.create_solid_color_brush(aspose.pydrawing.Color.blue)
36        
37        # Add path4 once again.
38        path5: XpsPath = doc.add_path(path4)
39        # path4 and path5 are not the same again ...
40        # (move path5 300 units lower)
41        path5.render_transform = path5.render_transform.clone() # to disconnect RenderTransform value from path4 (see next comment about Fill property)
42        path5.render_transform.translate(0, 300)
43        # ... but if we set the opacity of Fill property, it will take effect on both path5 and path4
44        # because brush is a complex property value which remains the same for path5 and path4
45        path5.fill.opacity = 0.8
46        
47        # Create a new path6 with path2's geometry ...
48        path6: XpsPath = doc.add_path(path2.data)
49        # ... shift it 600 units to the right ...
50        path6.render_transform = doc.create_matrix(1, 0, 0, 1, 600, 0)
51        # ... and set a yellow solid fill
52        path6.fill = doc.create_solid_color_brush(aspose.pydrawing.Color.yellow)
53        
54        # Now add path6's clone ...
55        path7: XpsPath = doc.add_path(path6.clone())
56        # (move path5 300 units lower)
57        path7.render_transform = path7.render_transform.clone()
58        path7.render_transform.translate(0, 300)
59        # ... and set the opacity for path7
60        path7.fill.opacity = 0.8
61        # Now opacity effects independantly as soon as property values are cloned along with the element
62        
63        # The following code block is equivalent to the previous one.
64        # Add path6 itself. path6 and path7 are not the same. Although their Fill property values are the same 
65        #XpsPath path7 = doc.add_path(path6);
66        #path7.RenderTransform = path7.RenderTransform.Clone();
67        #path7.RenderTransform.Translate(0, 300);
68        # To "disconnect" path7's Fill property from path6's Fill property reassign it to its clone (or path6's Fill clone)
69        #path7.Fill = ((XpsSolidColorBrush)path7.Fill).Clone();
70        #path7.Fill.Opacity = 0.8f;
71        
72        # Save the resultant XPS document
73        doc.save(data_dir + "WorkingWithTransparency_out.xps")

请参阅 .NETJavaC++ 中关于 XPS 文档透明度的用法。


结果

Opacity Mask

设置不透明度蒙版

解决方案提供了 set_opacity_mask() 属性,用于将不透明度蒙版应用于 XPS 文档。为此,您必须创建一个 PathGeometry 并将其添加到 XpsPath。图像可以用作不透明蒙版,其中每个像素的 Alpha 分量决定了应用于底层填充的透明度级别。生成的 XPS 文档将显示覆盖源图像文件的倾斜渐变条纹。以下代码片段演示了设置不透明蒙版的完整功能:

 1# The path to the documents directory.
 2data_dir = Util.get_data_dir_working_with_transparency()
 3# Create a new XPS Document
 4doc = XpsDocument()
 5#Add Canvas to the XpsDocument instance
 6canvas = doc.add_canvas()
 7# A rectangle with opacity masked by ImageBrush
 8path = canvas.add_path(doc.create_path_geometry("M 10,180 L 228,180 228,285 10,285"))
 9path.fill = doc.create_solid_color_brush(doc.create_color(1.0, 0.0, 0.0))
10imageBrush: XpsImageBrush = doc.create_image_brush(data_dir + "R08SY_NN.tif", aspose.pydrawing.RectangleF(0, 0, 128, 192),
11        aspose.pydrawing.RectangleF(0, 0, 64, 96))
12path.opacity_mask = imageBrush
13imageBrush.tile_mode = XpsTileMode.TILE
14# Save the resultant XPS document
15doc.save(data_dir + "OpacityMask_out.xps")

请参阅 .NETJavaC++ 中 XPS 文档透明度的使用方法。


结果

使用透明度

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

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.