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

在 XPS 文档中添加透明对象

Aspose.Page for Java 提供了 XpsPath 类,您可以使用它在 XPS 文档中添加透明对象。您需要指定 PathGeometry 并将其添加到 XpsPath。以下代码片段展示了在 XPS 文档中添加透明对象的完整功能:

 1// Demonstrates transparency in shapes in XPS document.
 2
 3// Create new XPS Document
 4XpsDocument doc = new XpsDocument();
 5
 6String outputFileName = "AddTransparentObject_outXPS.xps";
 7
 8// Just to demonstrate transparency - draw background rectangles
 9doc.addPath(doc.createPathGeometry("M120,0 H400 v1000 H120")).setFill(doc.createSolidColorBrush(doc.createColor(128,128,128)));
10doc.addPath(doc.createPathGeometry("M300,120 h600 V420 h-600")).setFill(doc.createSolidColorBrush(doc.createColor(128,128,128)));
11
12// Create path with closed rectangle geometry
13XpsPath path1 = doc.createPath(doc.createPathGeometry("M20,20 h200 v200 h-200 z"));
14// Set blue solid brush to fill path1
15path1.setFill(doc.createSolidColorBrush(doc.createColor(0, 0, 255)));
16// Add it to the current page
17XpsPath path2 = doc.add(path1);
18
19// path1 and path2 are the same as long as path1 has no parent. Setting fill on path2 affects shared brush
20path2.setFill(doc.createSolidColorBrush(doc.createColor(0, 128, 0)));
21
22// Now add path2 once again. Now path2 has parent. So path3 won't be the same as path2.
23XpsPath path3 = doc.add(path2);
24// shift it 300 units lower
25path3.setRenderTransform(doc.createMatrix(1f, 0f, 0f, 1f, 0f, 300f));
26// set red solid brush to fill it
27path3.setFill(doc.createSolidColorBrush(doc.createColor(255, 0, 0)));
28
29// Create new path4 with path2's geometry ...
30XpsPath path4 = doc.addPath(path2.getData());
31// shift it 300 units to the right ...
32path4.setRenderTransform(doc.createMatrix(1f, 0f, 0f, 1f, 300f, 0f));
33// and set blue solid fill
34path4.setFill(doc.createSolidColorBrush(doc.createColor(0, 0, 255)));
35
36// Add path4 once again.
37XpsPath path5 = doc.add(path4);
38// move path5 300 units lower and disconnect render transform from path4
39path5.setRenderTransform(path5.getRenderTransform().deepClone());
40path5.getRenderTransform().translate(0f, 300f);
41// set the opacity of Fill property - this will affect both path5 and path4 if brush instance is shared
42path5.getFill().setOpacity(0.8f);
43
44// Create new path6 with path2's geometry ...
45XpsPath path6 = doc.addPath(path2.getData());
46// shift it 600 units to the right ...
47path6.setRenderTransform(doc.createMatrix(1f, 0f, 0f, 1f, 600f, 0f));
48// and set yellow solid fill
49path6.setFill(doc.createSolidColorBrush(doc.createColor(255, 255, 0)));
50
51// Now add path6's clone ...
52XpsPath path7 = doc.add(path6.deepClone());
53// move it 300 units lower
54path7.setRenderTransform(path7.getRenderTransform().deepClone());
55path7.getRenderTransform().translate(0f, 300f);
56// set opacity for path7
57path7.getFill().setOpacity(0.8f);
58
59// Save resultant XPS document
60doc.save(getOutputDir() + outputFileName);

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


结果

Opacity Mask

设置不透明蒙版

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

 1// Apply opacity mask for texture pattern in XPS document.
 2
 3// Create new XPS Document
 4XpsDocument doc = new XpsDocument();
 5
 6String outputFileName = "OpacityMask_out.xps";
 7
 8//Add Canvas to XpsDocument instance
 9XpsCanvas canvas = doc.addCanvas();
10// Rectangle with opacity masked by ImageBrush
11XpsPath path = canvas.addPath(doc.createPathGeometry("M 10,180 L 228,180 228,285 10,285"));
12path.setFill(doc.createSolidColorBrush(doc.createColor(255, 0, 0)));
13path.setOpacityMask((XpsImageBrush)doc.createImageBrush(getDataDir() + "R08SY_NN.tif", new Rectangle2D.Float(0f, 0f, 128f, 192f),
14        new Rectangle2D.Float(0f, 0f, 64f, 96f)));
15((XpsImageBrush)path.getOpacityMask()).setTileMode(XpsTileMode.Tile);
16// Save resultant XPS document
17doc.save(getOutputDir() + outputFileName);

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


结果

Working With Transparency

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

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.