Working with Visual Brush in XPS file | Java
Contents
[
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 https://github.com/aspose-page/Aspose.Page-for-Java
2// The path to the documents directory.
3String dataDir = Utils.getDataDir();
4
5// Create new XPS Document
6XpsDocument doc = new XpsDocument();
7
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)));
13
14// Geometry for magenta grid VisualBrush
15XpsPathGeometry pathGeometry = doc.createPathGeometry();
16pathGeometry.addSegment(doc.createPolyLineSegment(
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));
19
20// Path for magenta grid
21XpsPath gridPath = doc.createPath(pathGeometry);
22gridPath.setFill(doc.createVisualBrush(visualCanvas,
23 new Rectangle2D.Float(0f, 0f, 10f, 10f), new Rectangle2D.Float(0f, 0f, 10f, 10f)));
24((XpsVisualBrush)gridPath.getFill()).setTileMode(XpsTileMode.Tile);
25
26// New canvas
27XpsCanvas canvas = doc.addCanvas();
28canvas.setRenderTransform(doc.createMatrix(1f, 0f, 0f, 1f, 268f, 70f));
29// Add grid
30canvas.addPath(gridPath);
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)));
34path.setOpacity(0.7f);
35// Save resultant XPS document
36doc.save(dataDir + "AddGrid_out.xps");
The result is next
You can download examples and data files from GitHub.