Working with Visual Brush in XPS file | .NET

Contents
[ Hide Show ]

Add Grid using Visual Brush

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

 1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
 2// The path to the documents directory.
 3string dataDir = RunExamples.GetDataDir_WorkingWithVisualBrush();
 4
 5XpsDocument doc = new XpsDocument();
 6// Geometry for magenta grid VisualBrush
 7XpsPathGeometry pathGeometry = doc.CreatePathGeometry();
 8pathGeometry.AddSegment(doc.CreatePolyLineSegment(
 9    new PointF[] { new PointF(240f, 5f), new PointF(240f, 310f), new PointF(0f, 310f) }));
10pathGeometry[0].StartPoint = new PointF(0f, 5f);
11
12// Canvas for magenta grid VisualBrush
13XpsCanvas visualCanvas = doc.CreateCanvas();
14
15XpsPath visualPath = visualCanvas.AddPath(
16    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"));
17visualPath.Fill = doc.CreateSolidColorBrush(doc.CreateColor(1f, .61f, 0.1f, 0.61f));
18
19XpsPath gridPath = doc.CreatePath(pathGeometry);
20//Create Visual Brush, it is specified by some XPS fragment (vector graphics and glyphs)
21gridPath.Fill = doc.CreateVisualBrush(visualCanvas,
22    new RectangleF(0f, 0f, 10f, 10f), new RectangleF(0f, 0f, 10f, 10f));
23((XpsVisualBrush)gridPath.Fill).TileMode = XpsTileMode.Tile;
24// New canvas
25XpsCanvas canvas = doc.AddCanvas();
26canvas.RenderTransform = doc.CreateMatrix(1f, 0f, 0f, 1f, 268f, 70f);
27// Add grid
28canvas.AddPath(gridPath); 
29// Red transparent rectangle in the middle top
30XpsPath path = canvas.AddPath(doc.CreatePathGeometry("M 30,20 l 258.24,0 0,56.64 -258.24,0 Z"));
31path = canvas.AddPath(doc.CreatePathGeometry("M 10,10 L 228,10 228,100 10,100"));
32path.Fill = doc.CreateSolidColorBrush(doc.CreateColor(1.0f, 0.0f, 0.0f));
33path.Opacity = 0.7f;
34// Save resultant XPS document
35doc.Save(dataDir + "AddGrid_out.xps");

See working with visual brush in XPS documents in Java 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.