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. The 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// Paint rectangle with horizontal gradient fill in XPS document.
 2
 3// Create new XPS Document
 4XpsDocument doc = new XpsDocument();
 5
 6String outputFileName = "HorizontalGradient_outXPS.xps";
 7
 8// Initialize List of XpsGradentStop
 9List<XpsGradientStop> stops = new ArrayList<>();
10stops.add(doc.createGradientStop(doc.createColor(255, 244, 253, 225), 0.0673828f));
11stops.add(doc.createGradientStop(doc.createColor(255, 251, 240, 23), 0.314453f));
12stops.add(doc.createGradientStop(doc.createColor(255, 252, 209, 0), 0.482422f));
13stops.add(doc.createGradientStop(doc.createColor(255, 241, 254, 161), 0.634766f));
14stops.add(doc.createGradientStop(doc.createColor(255, 53, 253, 255), 0.915039f));
15stops.add(doc.createGradientStop(doc.createColor(255, 12, 91, 248), 1f));
16// Create new path by defining geometery in abbreviation form
17XpsPath path = 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);
21// Save resultant XPS document
22doc.save(getOutputDir() + outputFileName);

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. The 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// Paint rectangle with vertical gradient fill in XPS document.
 2
 3// Create new XPS Document
 4XpsDocument doc = new XpsDocument();
 5
 6String outputFileName = "VerticalGradient_outXPS.xps";
 7
 8// Initialize List of XpsGradentStop
 9List<XpsGradientStop> stops = new ArrayList<>();
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// Create new path by defining geometery in abbreviation form
16XpsPath path = 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);
20// Save resultant XPS document
21doc.save(getOutputDir() + outputFileName);

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. The following code snippet shows complete functionality to add a linear gradient on an XPS document:

 1// Paint rectangle with diagonal gradient fill in XPS document.
 2
 3// Create new XPS Document
 4XpsDocument doc = new XpsDocument();
 5
 6String outputFileName = "DiagonalGradient_outXPS.xps";
 7
 8// Initialize List of XpsGradentStop
 9List<XpsGradientStop> stops = new ArrayList<>();
10// Add Colors to Gradient
11stops.add(doc.createGradientStop(doc.createColor(0, 142, 4), 0f));
12stops.add(doc.createGradientStop(doc.createColor(255, 202, 0), 0.144531f));
13stops.add(doc.createGradientStop(doc.createColor(255, 250, 0), 0.264648f));
14stops.add(doc.createGradientStop(doc.createColor(255, 0, 0), 0.414063f));
15stops.add(doc.createGradientStop(doc.createColor(233, 0, 255), 0.544922f));
16stops.add(doc.createGradientStop(doc.createColor(107, 27, 190), 0.694336f));
17stops.add(doc.createGradientStop(doc.createColor(63, 0, 255), 0.844727f));
18stops.add(doc.createGradientStop(doc.createColor(0, 199, 80), 1f));
19// Create new path by defining geometery in abbreviation form
20XpsPath path = doc.addPath(doc.createPathGeometry("M 10,10 L 228,10 228,100 10,100"));
21path.setRenderTransform(doc.createMatrix(1f, 0f, 0f, 1f, 20f, 70f));
22path.setFill(doc.createLinearGradientBrush(new Point2D.Float(10f, 10f), new Point2D.Float(228f, 100f)));
23((XpsGradientBrush)path.getFill()).getGradientStops().addAll(stops);
24// Save resultant XPS document
25doc.save(getOutputDir() + outputFileName);

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


The result

Add Diagonal Gradient

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

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.