Working with Gradient in XPS file | Java

Add Gradient in XPS Document

Add Horizontal Gradient

Aspose.Page for Java offers XpsGradientBrush Class, with which you can add gradient on XPS document. You need to specify XpsGradientStop and add XpsPath to the object of XpsDocument class. Following code snippet shows complete functionality to add horizontal gradient on an XPS document:

See working with gradient in XPS documents in .NET and C++.


The result

Add Horizontal Gradient

 1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
 2// The path to the documents directory.
 3String dataDir = Utils.getDataDir();
 4//Initialize document
 5XpsDocument doc = new XpsDocument();
 6        
 7// Horizontal gradient
 8List<XpsGradientStop> stops = new LinkedList<XpsGradientStop>();
 9stops.add(doc.createGradientStop(doc.createColor(255, 244, 253, 225), 0.0673828f));
10stops.add(doc.createGradientStop(doc.createColor(255, 251, 240, 23), 0.314453f));
11stops.add(doc.createGradientStop(doc.createColor(255, 252, 209, 0), 0.482422f));
12stops.add(doc.createGradientStop(doc.createColor(255, 241, 254, 161), 0.634766f));
13stops.add(doc.createGradientStop(doc.createColor(255, 53, 253, 255), 0.915039f));
14stops.add(doc.createGradientStop(doc.createColor(255, 12, 91, 248), 1f));
15
16XpsPath path = doc.addPath(doc.createPathGeometry("M 30,20 l 258.24,0 0,56.64 -258.24,0 Z"));
17path = doc.addPath(doc.createPathGeometry("M 10,210 L 228,210 228,300 10,300"));
18path.setRenderTransform(doc.createMatrix(1f, 0f, 0f, 1f, 20f, 70f));
19path.setFill(doc.createLinearGradientBrush(new Point2D.Float(10f, 0f), new Point2D.Float(228f, 0f)));
20((XpsGradientBrush)path.getFill()).getGradientStops().addAll(stops);
21stops.clear();
22
23doc.save(dataDir + "HorizontalGradient.xps");    

Add Vertical Gradient

Aspose.Page for Java offers XpsGradientBrush Class, with which you can add gradient on XPS document. You need to specify XpsGradientStop and add XpsPath to the object of XpsDocument class. Following code snippet shows complete functionality to add vertical gradient on an XPS document:

See working with gradient in XPS documents in .NET and C++.


The result

Add Vertical Gradient

 1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
 2//Initialize document
 3XpsDocument doc = new XpsDocument();
 4XpsPath path = doc.addPath(doc.createPathGeometry("M 30,20 l 258.24,0 0,56.64 -258.24,0 Z"));
 5
 6// Vertical gradient
 7List<XpsGradientStop> stops = new LinkedList<XpsGradientStop>();
 8 
 9// Vertical gradient bellow
10stops.add(doc.createGradientStop(doc.createColor(253, 255, 12, 0), 0f));
11stops.add(doc.createGradientStop(doc.createColor(252, 255, 154, 0), 0.359375f));
12stops.add(doc.createGradientStop(doc.createColor(252, 255, 56, 0), 0.424805f));
13stops.add(doc.createGradientStop(doc.createColor(253, 255, 229, 0), 0.879883f));
14stops.add(doc.createGradientStop(doc.createColor(252, 255, 255, 234), 1f));
15
16path = doc.addPath(doc.createPathGeometry("M 10,110 L 228,110 228,200 10,200"));
17path.setRenderTransform(doc.createMatrix(1f, 0f, 0f, 1f, 20f, 70f));
18path.setFill(doc.createLinearGradientBrush(new Point2D.Float(10f, 110f), new Point2D.Float(10f, 200f)));
19((XpsGradientBrush)path.getFill()).getGradientStops().addAll(stops);
20stops.clear();
21        
22doc.save(dataDir + "VrticalGradient.xps");

Add Diagonal Gradient

Aspose.Page for Java offers XpsGradientBrush Class, with which you can add gradient on XPS document. You need to specify XpsGradientStop and add XpsPath to the object of XpsDocument class. Following code snippet shows complete functionality to add a linear gradient on an XPS document:

 1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
 2// The path to the documents directory.
 3String dataDir = Utils.getDataDir();
 4
 5
 6XpsDocument doc = new XpsDocument();
 7
 8XpsPath path = doc.addPath(doc.createPathGeometry("M 30,20 l 258.24,0 0,56.64 -258.24,0 Z"));
 9
10// Linear gradient
11List<XpsGradientStop> stops = new LinkedList<XpsGradientStop>();
12stops.add(doc.createGradientStop(doc.createColor(0, 142, 4), 0f));
13stops.add(doc.createGradientStop(doc.createColor(255, 202, 0), 0.144531f));
14stops.add(doc.createGradientStop(doc.createColor(255, 250, 0), 0.264648f));
15stops.add(doc.createGradientStop(doc.createColor(255, 0, 0), 0.414063f));
16stops.add(doc.createGradientStop(doc.createColor(233, 0, 255), 0.544922f));
17stops.add(doc.createGradientStop(doc.createColor(107, 27, 190), 0.694336f));
18stops.add(doc.createGradientStop(doc.createColor(63, 0, 255), 0.844727f));
19stops.add(doc.createGradientStop(doc.createColor(0, 199, 80), 1f));
20
21path = doc.addPath(doc.createPathGeometry("M 10,10 L 228,10 228,100 10,100"));
22path.setRenderTransform(doc.createMatrix(1f, 0f, 0f, 1f, 20f, 70f));
23path.setFill(doc.createLinearGradientBrush(new Point2D.Float(10f, 10f), new Point2D.Float(228f, 100f)));
24((XpsGradientBrush)path.getFill()).getGradientStops().addAll(stops);
25stops.clear();
26
27doc.save(dataDir + "LinearGradient.xps");

See working with gradient in XPS documents in .NET and C++.


The result

Add Diagonal Gradient

You can download examples and data files from GitHub.

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.