Manage XPS graphic utilities and visual elements in Java

Construct, organize, and transform XML Paper Specification (XPS) vector graphics and visual elements using Aspose.Page for Java. Build sophisticated XPS document layouts by utilizing core graphic utilities—including canvas containers (XpsCanvas), vector path objects (XpsPath), geometric primitives (XpsGeometry), matrix transformations, and visual brushes—without relying on native Windows XPS print subsystems or external software dependencies.

 

Aspose.Page for Java provides a structured object model for XPS visual trees, mirroring the native XML Paper Specification markup hierarchy while offering high-performance Java manipulation.

  • To group multiple visual elements (XpsPath, XpsGlyphs, child canvases) and apply shared opacity or matrix transformations use XpsCanvas as a grouping container.
  • To define filled or stroked geometric vector shapes using paths, lines, arcs, and Bézier curves use XpsPath as a vector path element.
  • To control affine transformations including translation, rotation, scaling, and skewing use XpsMatrix coordinate transformation.
  • To hold geometric coordinate path data (XpsPathGeometry, XpsPathFigure, XpsRectangleGeometry) use XpsGeometry as shape definition.
  • To fill paths or glyphs using solid colors (XpsSolidColorBrush), gradients, or visual patterns use XpsBrush visual surface fill.

Grouping Graphic Elements with Canvas (`XpsCanvas`)

Group multiple vector shapes, text elements, or child containers into a unified canvas layer to apply collective opacity, clipping masks, or coordinate transformations.

import com.aspose.page.xps.XpsDocument;
import com.aspose.page.xps.XpsCanvas;
import com.aspose.page.xps.XpsPath;
import java.awt.Color;

public class CreateXpsCanvasGroup {
    public static void main(String[] args) throws Exception {
        // Initialize a new XPS document
        XpsDocument doc = new XpsDocument();

        // Create a root canvas container
        XpsCanvas mainCanvas = doc.addCanvas();

        // Create a child canvas layer with 50% opacity
        XpsCanvas groupedLayer = mainCanvas.addCanvas();
        groupedLayer.setOpacity(0.5f);

        // Add vector shapes to the grouped canvas
        XpsPath rect1 = groupedLayer.addPath(doc.createGeometry("M 100,100 L 300,100 L 300,200 L 100,200 Z"));
        rect1.setFill(doc.createSolidColorBrush(Color.BLUE));

        XpsPath rect2 = groupedLayer.addPath(doc.createGeometry("M 150,150 L 350,150 L 350,250 L 150,250 Z"));
        rect2.setFill(doc.createSolidColorBrush(Color.RED));

        // Save compiled XPS document
        doc.save("C:/XPSProject/Output/grouped_canvas.xps");

        System.out.println("XPS document with grouped canvas saved successfully.");
    }
}

Creating Custom Geometries & Vector Paths (`XpsPath`)

Build complex multi-segment path geometries using abbreviated path data syntax or by manually constructing XpsPathFigure segments.

import com.aspose.page.xps.XpsDocument;
import com.aspose.page.xps.XpsPath;
import java.awt.Color;

public class CustomXpsGeometry {
    public static void main(String[] args) throws Exception {
        XpsDocument doc = new XpsDocument();

        // Define path geometry using standard XPS mini-language syntax
        String pathData = "M 100,200 C 100,100 250,100 250,200 S 400,300 400,200";

        // Create path element with geometry
        XpsPath customCurve = doc.addPath(doc.createGeometry(pathData));

        // Configure stroke paint and thickness
        customCurve.setStroke(doc.createSolidColorBrush(Color.DARK_GRAY));
        customCurve.setStrokeThickness(4.0f);

        // Save document
        doc.save("C:/XPSProject/Output/custom_geometry.xps");

        System.out.println("Custom XPS vector geometry rendered successfully.");
    }
}

Applying Matrix Transformations (`XpsMatrix`)

Apply affine matrix transformations to individual paths or whole canvas containers to scale, rotate, translate, or shear vector elements within the XPS document layout.

import com.aspose.page.xps.XpsDocument;
import com.aspose.page.xps.XpsCanvas;
import com.aspose.page.xps.XpsPath;
import com.aspose.page.xps.XpsMatrix;
import java.awt.Color;

public class TransformXpsElements {
    public static void main(String[] args) throws Exception {
        XpsDocument doc = new XpsDocument();

        // Create canvas container
        XpsCanvas transformCanvas = doc.addCanvas();

        // Initialize matrix transformation (Rotate 30 degrees around center 200, 200)
        XpsMatrix matrix = doc.createMatrix();
        matrix.rotate(30.0f);
        matrix.translate(100.0f, 50.0f);

        // Apply matrix to canvas
        transformCanvas.setRenderTransform(matrix);

        // Add vector shape to transformed canvas
        XpsPath rect = transformCanvas.addPath(doc.createGeometry("M 100,100 L 300,100 L 300,300 L 100,300 Z"));
        rect.setFill(doc.createSolidColorBrush(Color.GREEN));

        // Save document
        doc.save("C:/XPSProject/Output/transformed_elements.xps");

        System.out.println("Transformed XPS graphic elements saved.");
    }
}

Installation and Setup

Add Aspose.Page for Java to your Maven:

Package Manager Console Command

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-page</artifactId>
    <version>Latest</version>
</dependency>

FAQ

1. Do I need Adobe Acrobat or PostScript driver utilities installed?

XpsCanvas is a container element used to group, mask, and transform visual objects together, whereas XpsPath represents a concrete renderable vector graphic element defined by geometric path boundaries and brush fills/strokes.

2. Does Aspose.Page for Java support standard WPF/XPS path syntax strings?

Yes. You can pass standard XPS path geometry data strings (e.g., ‘M 10,10 L 100,100 Z’) directly to doc.createGeometry() to instantiate XpsPathGeometry objects rapidly.

3. Can I apply clipping regions to an XpsCanvas or XpsPath?

Yes. Both XpsCanvas and XpsPath provide a .setClip() method that accepts an XpsPathGeometry instance to constrain all nested or element rendering within a closed vector boundary.