Travailler avec la transparence dans un fichier XPS | Java
Ajouter un objet transparent dans le document XPS
Aspose.Page pour Java propose la classe XpsPath , avec laquelle vous pouvez ajouter un objet transparent sur un document XPS. Vous devez spécifier PathGeometry et l’ajouter à XpsPath. L’extrait de code suivant montre la fonctionnalité complète permettant d’ajouter un objet transparent sur un document 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);Le résultat

Définir le masque d’opacité
La solution propose la propriété setOpacityMask() , avec laquelle vous pouvez définir un masque d’opacité sur un document XPS. Vous devez créer PathGeometry et l’ajouter à XpsPath. Une image peut être utilisée comme masque d’opacité et le composant Alpha de chaque pixel est utilisé pour l’appliquer sur le remplissage sous-jacent. Le document XPS généré affichera des bandes de dégradé obliques comme présent sur le fichier image source. L’extrait de code suivant montre la fonctionnalité complète permettant de définir le masque d’opacité :
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);Le résultat

Vous pouvez télécharger des exemples et des fichiers de données à partir de GitHub.