Working with Visual Brush in XPS file | Java

[ Hide Show ]

Add Grid using Visual Brush

Aspose.Page for Java offers XpsVisualBrush Class, with which you can add a grid on XPS document. You need to specify XpsPathGeometry and add XpsCanvas to the object of XpsDocument class. Following code snippet shows complete functionality to add a grid on an XPS document:

 1// For complete examples and data files, please go to
 2// The path to the documents directory.
 3String dataDir = Utils.getDataDir();
 5// Create new XPS Document
 6XpsDocument doc = new XpsDocument();
 8// Canvas for magenta grid VisualBrush
 9XpsCanvas visualCanvas = doc.createCanvas();
10XpsPath visualPath = visualCanvas.addPath(
11                doc.createPathGeometry("M 0,4 L 4,4 4,0 6,0 6,4 10,4 10,6 6,6 6,10 4,10 4,6 0,6 Z"));
12visualPath.setFill(doc.createSolidColorBrush(doc.createColor(1f, .61f, 0.1f, 0.61f)));
14// Geometry for magenta grid VisualBrush
15XpsPathGeometry pathGeometry = doc.createPathGeometry();
17                new Point2D.Float[] { new Point2D.Float(240f, 5f), new Point2D.Float(240f, 310f), new Point2D.Float(0f, 310f) }));
18pathGeometry.getFigure(0).setStartPoint(new Point2D.Float(0f, 5f));
20// Path for magenta grid
21XpsPath gridPath = doc.createPath(pathGeometry);
23                new Rectangle2D.Float(0f, 0f, 10f, 10f), new Rectangle2D.Float(0f, 0f, 10f, 10f)));
26// New canvas
27XpsCanvas canvas = doc.addCanvas();
28canvas.setRenderTransform(doc.createMatrix(1f, 0f, 0f, 1f, 268f, 70f));
29// Add grid
31// Red transparent rectangle in the middle top
32XpsPath path = canvas.addPath(doc.createPathGeometry("M 10,10 L 228,10 228,100 10,100"));
33path.setFill(doc.createSolidColorBrush(doc.createColor(1.0f, 0.0f, 0.0f)));
35// Save resultant XPS document + "AddGrid_out.xps");

See working with visual brush in XPS documents in .NET and C++.

The result is next

Add Grid

You can download examples and data files from GitHub.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.