Lavorare con il gradiente in un file XPS | Java
Aggiungi sfumatura in un documento XPS
Aggiungi sfumatura orizzontale
Aspose.Page per Java offre la classe XpsGradientBrush, con la quale è possibile aggiungere una sfumatura a un documento XPS. È necessario specificare XpsGradientStop e aggiungere XpsPath all’oggetto della classe XpsDocument. Il seguente frammento di codice mostra la funzionalità completa per aggiungere una sfumatura orizzontale a un documento XPS:
Il risultato

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);Aggiungi sfumatura verticale
Aspose.Page per Java offre la classe XpsGradientBrush, con la quale è possibile aggiungere una sfumatura a un documento XPS. È necessario specificare XpsGradientStop e aggiungere XpsPath all’oggetto della classe XpsDocument. Il seguente frammento di codice mostra la funzionalità completa per aggiungere una sfumatura verticale a un documento XPS:
Il risultato

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);Aggiungi sfumatura diagonale
Aspose.Page per Java offre la classe XpsGradientBrush, con la quale è possibile aggiungere una sfumatura a un documento XPS. È necessario specificare XpsGradientStop e aggiungere XpsPath all’oggetto della classe XpsDocument. Il seguente frammento di codice mostra la funzionalità completa per aggiungere una sfumatura lineare a un documento XPS:
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);Risultato

È possibile scaricare esempi e file di dati da GitHub.