Work with canvases within XPS
Clip and transform canvases of XPS files using API for .NET
Managing Canvases in the documents is one of the features offered by Aspose.Page for .NET. This is a solution for working with different Page Description Languages, XPS XPS in specific. In the example below you will find out how to:
Create a canvas in an XPS file.
Make left and top offsets in the main canvas.
Add a new canvas with a translated transformation to the main canvas.
Add a new canvas with a rotation around a point transformation to the main canvas.
Clip canvas. And many more manipulations with canvases in XPS files.
To transform canvases of XPS file follow the next guide:
- Create an XPS file using the XpsDocument Class .
- Create the main canvas, common for all page elements with the AddCanvas() Method.
- Make left and top offsets in the main canvas using the CreateMatrix() Method.
- Create rectangle path geometry with the CreatePathGeometry() Method.
- Create a fill for rectangles by means of the XpsBrush Class.
- To create a rectangle in canvas 2 and fill it use the XpsPath Class.
- To translate canvas 3 to position a new rectangle below the previous rectangle use the CreateMatrix() Method.
- To translate this canvas to right side of page the Translate() Method.
- To scale canvas 4 call the Scale() Method.
- To rotate canvas 5 around a point of 45 degrees the RotateAround() Method goes in handy.
- Save the changed XPS document using the XPsDocument.Save() Method.
XPS file Canvas Transformation C# code
using Aspose.Page.Xps;
using Aspose.Page.Xps.XpsModel;
using System.Drawing;
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithCanvas();
// Create a new XPS Document
XpsDocument doc = new XpsDocument();
// Create main canvas, common for all page elemnts
XpsCanvas canvas1 = doc.AddCanvas();
// Make left and top offsets in the main canvas
canvas1.RenderTransform = doc.CreateMatrix(1, 0, 0, 1, 20, 10);
// Create the rectangle path geometry
XpsPathGeometry rectGeom = doc.CreatePathGeometry("M 0,0 L 200,0 200,100 0,100 Z");
// Create a fill for rectangles
XpsBrush fill = doc.CreateSolidColorBrush(doc.CreateColor(12, 15, 159));
// Add new canvas without any transformations to the main canvas
XpsCanvas canvas2 = canvas1.AddCanvas();
// Create rectangle in this canvas and fill it
XpsPath rect = canvas2.AddPath(rectGeom);
rect.Fill = fill;
// Add new canvas with translate transformation to the main canvas
XpsCanvas canvas3 = canvas1.AddCanvas();
// Translate this canvas to position new rectangle below the previous rectnagle
canvas3.RenderTransform = doc.CreateMatrix(1, 0, 0, 1, 0, 200);
// Translate this canvas to the right side of the page
canvas3.RenderTransform.Translate(500, 0);
// Create the rectangle in this canvas and fill it
rect = canvas3.AddPath(rectGeom);
rect.Fill = fill;
// Add new canvas with the double scale transformation to the main canvas
XpsCanvas canvas4 = canvas1.AddCanvas();
// Translate this canvas to position new rectangle below the previous rectnagle
canvas4.RenderTransform = doc.CreateMatrix(1, 0, 0, 1, 0, 400);
// Scale this canvas
canvas4.RenderTransform.Scale(2, 2);
// Create a rectangle in this canvas and fill it
rect = canvas4.AddPath(rectGeom);
rect.Fill = fill;
// Add new canvas with rotation around a point transformation to the main canvas
XpsCanvas canvas5 = canvas1.AddCanvas();
// Translate this canvas to position new rectangle below the previous rectnagle
canvas5.RenderTransform = doc.CreateMatrix(1, 0, 0, 1, 0, 800);
// Rotate this canvas aroud a point on 45 degrees
canvas5.RenderTransform.RotateAround(45, new PointF(100, 50));
rect = canvas5.AddPath(rectGeom);
rect.Fill = fill;
// Save the resultant XPS document
doc.Save(dataDir + "output1.xps");
To Clip canvases of XPS file follow the next guide:
- Create or open an XPS file using XpsDocument Class.
- Create the main canvas, common for all page elements with the AddCanvas() Method.
- Make left and top offsets in the main canvas using the CreateMatrix() Method.
- Create rectangle path geometry with the CreatePathGeometry() Method.
- Create a fill for rectangles by means of the XpsBrush Class.
- To add another canvas with a clip to the main canvas call the AddCanvas() Method again.
- Create circle geometry for clip using the XpsPathGeometry Class.
- To create a rectangle in this canvas and fill it use the XpsPath Class.
- Add another canvas with the AddCanvas() Method then create a rectangle in this canvas and stroke it with the XpsPathGeometry Class.
- Save the changed XPS document by means of the XPsDocument.Save() Method.
XPS file canvas clipping C# code
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithCanvas();
// Create a new XPS Document
XpsDocument doc = new XpsDocument();
// Create main canvas, common for all page elemnts
XpsCanvas canvas1 = doc.AddCanvas();
// Make left and top offsets in the main canvas
canvas1.RenderTransform = doc.CreateMatrix(1, 0, 0, 1, 20, 10);
// Create the rectangle path geometry
XpsPathGeometry rectGeom = doc.CreatePathGeometry("M 0,0 L 500,0 500,300 0,300 Z");
// Create a fill for rectangles
XpsBrush fill = doc.CreateSolidColorBrush(doc.CreateColor(12, 15, 159));
// Add another canvas with the clip to the main canvas
XpsCanvas canvas2 = canvas1.AddCanvas();
// Create circle geometry for the clip
XpsPathGeometry clipGeom = doc.CreatePathGeometry("M250,250 A100,100 0 1 1 250,50 100,100 0 1 1 250,250");
canvas2.Clip = clipGeom;
// Create rectangle in this canvas and fill it
XpsPath rect = canvas2.AddPath(rectGeom);
rect.Fill = fill;
// Add the second canvas with stroked rectangle to the main canvas
XpsCanvas canvas3 = canvas1.AddCanvas();
// Create rectangle in this canvas and stroke it
rect = canvas3.AddPath(rectGeom);
rect.Stroke = fill;
rect.StrokeThickness = 2;
// Save resultant XPS document
doc.Save(dataDir + "output2.xps");
FAQ
1. How can I add a canvas to an XPS file?
Set the path to the documents directory. To add canvases use the AddCanvas() Method.
2. How to manipulate canvas offsets in an XPS file?
Set the path to the documents directory and add canvases. Use the CreateMatrix() Method to make offsets.
3. What operations with canvases in the XPS file are supported?
You can create, clip, and add a canvas as well as manipulate its offsets.
XPS What is XPS File Format
XPS format is similar to PDF format. Both are page description language (PDL) formats. EPS is based on HTML and not on PostScript language. The .eps file is capable to contain a markup of the document's structure along with the information on how the document would look like. There are also added instructions on how to print and render the document. The feature of the format is that it fixes the document's description which means that it will look the same no matter who and from what operational system opens it.