|
// Demonstrates transparency in shapes in XPS document.
|
|
// Learn more: https://docs.aspose.com/page/java/xps/working-with-transparency/
|
|
|
|
// Create new XPS Document
|
|
XpsDocument doc = new XpsDocument();
|
|
|
|
String outputFileName = "AddTransparentObject_outXPS.xps";
|
|
|
|
// Just to demonstrate transparency - draw background rectangles
|
|
doc.addPath(doc.createPathGeometry("M120,0 H400 v1000 H120")).setFill(doc.createSolidColorBrush(doc.createColor(128,128,128)));
|
|
doc.addPath(doc.createPathGeometry("M300,120 h600 V420 h-600")).setFill(doc.createSolidColorBrush(doc.createColor(128,128,128)));
|
|
|
|
// Create path with closed rectangle geometry
|
|
XpsPath path1 = doc.createPath(doc.createPathGeometry("M20,20 h200 v200 h-200 z"));
|
|
// Set blue solid brush to fill path1
|
|
path1.setFill(doc.createSolidColorBrush(doc.createColor(0, 0, 255)));
|
|
// Add it to the current page
|
|
XpsPath path2 = doc.add(path1);
|
|
|
|
// path1 and path2 are the same as long as path1 has no parent. Setting fill on path2 affects shared brush
|
|
path2.setFill(doc.createSolidColorBrush(doc.createColor(0, 128, 0)));
|
|
|
|
// Now add path2 once again. Now path2 has parent. So path3 won't be the same as path2.
|
|
XpsPath path3 = doc.add(path2);
|
|
// shift it 300 units lower
|
|
path3.setRenderTransform(doc.createMatrix(1f, 0f, 0f, 1f, 0f, 300f));
|
|
// set red solid brush to fill it
|
|
path3.setFill(doc.createSolidColorBrush(doc.createColor(255, 0, 0)));
|
|
|
|
// Create new path4 with path2's geometry ...
|
|
XpsPath path4 = doc.addPath(path2.getData());
|
|
// shift it 300 units to the right ...
|
|
path4.setRenderTransform(doc.createMatrix(1f, 0f, 0f, 1f, 300f, 0f));
|
|
// and set blue solid fill
|
|
path4.setFill(doc.createSolidColorBrush(doc.createColor(0, 0, 255)));
|
|
|
|
// Add path4 once again.
|
|
XpsPath path5 = doc.add(path4);
|
|
// move path5 300 units lower and disconnect render transform from path4
|
|
path5.setRenderTransform(path5.getRenderTransform().deepClone());
|
|
path5.getRenderTransform().translate(0f, 300f);
|
|
// set the opacity of Fill property - this will affect both path5 and path4 if brush instance is shared
|
|
path5.getFill().setOpacity(0.8f);
|
|
|
|
// Create new path6 with path2's geometry ...
|
|
XpsPath path6 = doc.addPath(path2.getData());
|
|
// shift it 600 units to the right ...
|
|
path6.setRenderTransform(doc.createMatrix(1f, 0f, 0f, 1f, 600f, 0f));
|
|
// and set yellow solid fill
|
|
path6.setFill(doc.createSolidColorBrush(doc.createColor(255, 255, 0)));
|
|
|
|
// Now add path6's clone ...
|
|
XpsPath path7 = doc.add(path6.deepClone());
|
|
// move it 300 units lower
|
|
path7.setRenderTransform(path7.getRenderTransform().deepClone());
|
|
path7.getRenderTransform().translate(0f, 300f);
|
|
// set opacity for path7
|
|
path7.getFill().setOpacity(0.8f);
|
|
|
|
// Save resultant XPS document
|
|
doc.save(getOutputDir() + outputFileName); |