Work with canvases within XPS

Clip and transform canvases of XPS files using API for C++

 

In XPS files, a canvas is a rectangular area on a page where visual content, such as text, images, and shapes, can be drawn. It serves as a container for these elements and provides a framework for organizing and layering them.   Canvases can be reused in different parts of the document or even in multiple documents and they provide a flexible way to structure and organize content within an XPS document. Key characteristics of canvases in XPS files:

  • Canvases can be nested within other canvases, creating a hierarchical structure that allows for complex layouts and grouping of elements.
  • Canvases can be transformed using various operations, such as translation, rotation, and scaling. This enables flexible positioning and resizing of content.
  • Canvases can be clipped to specific regions, limiting the visible area of their contents. This is useful for creating intricate shapes and effects.
  • Canvases can contain different visual elements, including text runs, paragraphs, and text boxes. They can also contain images, geometrical shapes, and other canvases.

Managing Canvases in the documents is one of the features offered by Aspose.Page for C++. This is a solution for working with different Page Description Languages, XPS XPS in specific.

To transform canvases of XPS file follow the next guide:

  1. Create an XPS file using the XpsDocument Class .
  2. Create the main canvas, common for all page elements with the AddCanvas() Method.
  3. Make left and top offsets in the main canvas using the CreateMatrix() Method.
  4. Create rectangle path geometry with the CreatePathGeometry() Method.
  5. Create a fill for rectangles by means of the XpsBrush Class.
  6. To create a rectangle in canvas 2 and fill it use the XpsPath Class.
  7. To translate canvas 3 to position a new rectangle below the previous rectangle use the CreateMatrix() Method.
  8. To translate this canvas to the right side of the page the Translate() Method.
  9. To scale canvas 4 call the Scale() Method.
  10. To rotate canvas 5 around a point of 45 degrees the RotateAround() Method goes in handy.
  11. Save the changed XPS document using the XPsDocument.Save() Method.
The canvas transformation
// The path to the documents directory.
System::String dataDir = RunExamples::GetDataDir_WorkingWithCanvas();
// Create new XPS Document
System::SharedPtr<XpsDocument> doc = System::MakeObject<XpsDocument>();
// Create main canvas, common for all page elemnts
System::SharedPtr<XpsCanvas> canvas1 = doc->AddCanvas();
// Make left and top offsets in the main canvas
canvas1->set_RenderTransform(doc->CreateMatrix(1.0f, 0.0f, 0.0f, 1.0f, 20.0f, 10.0f));
// Create rectangle path geometry
System::SharedPtr<XpsPathGeometry> rectGeom = doc->CreatePathGeometry(u"M 0,0 L 200,0 200,100 0,100 Z");
// Create a fill for rectangles
System::SharedPtr<XpsBrush> fill = doc->CreateSolidColorBrush(doc->CreateColor(12, 15, 159));
// Add new canvas without any transformations to the main canvas
System::SharedPtr<XpsCanvas> canvas2 = canvas1->AddCanvas();
// Create rectangle in this canvas and fill it
System::SharedPtr<XpsPath> rect = canvas2->AddPath(rectGeom);
rect->set_Fill(fill);
// Add new canvas with translate transformation to the main canvas
System::SharedPtr<XpsCanvas> canvas3 = canvas1->AddCanvas();
// Translate this canvas to position new rectangle below previous rectnagle
canvas3->set_RenderTransform(doc->CreateMatrix(1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 200.0f));
// Translate this canvas to right side of page
canvas3->get_RenderTransform()->Translate(500.0f, 0.0f);
// Create rectangle in this canvas and fill it
rect = canvas3->AddPath(rectGeom);
rect->set_Fill(fill);
// Add new canvas with double scale transformation to the main canvas
System::SharedPtr<XpsCanvas> canvas4 = canvas1->AddCanvas();
// Translate this canvas to position new rectangle below previous rectnagle
canvas4->set_RenderTransform(doc->CreateMatrix(1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 400.0f));
// Scale this canvas
canvas4->get_RenderTransform()->Scale(2.0f, 2.0f);
// Create rectangle in this canvas and fill it
rect = canvas4->AddPath(rectGeom);
rect->set_Fill(fill);
// Add new canvas with rotation around a point transformation to the main canvas
System::SharedPtr<XpsCanvas> canvas5 = canvas1->AddCanvas();
// Translate this canvas to position new rectangle below previous rectnagle
canvas5->set_RenderTransform(doc->CreateMatrix(1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 800.0f));
// Rotate this canvas aroud a point on 45 degrees
canvas5->get_RenderTransform()->RotateAround(45.0f, System::Drawing::PointF(100.0f, 50.0f));
rect = canvas5->AddPath(rectGeom);
rect->set_Fill(fill);
// Save resultant XPS document
doc->Save(dataDir + u"output/" + u"output1.xps");
The next code snippet shows how to clip canvases of XPS files within the Aspose.Page for C++ Api Solution.

To Clip canvases of XPS file follow the next guide:

  1. Create or open an XPS file using XpsDocument Class.
  2. Create the main canvas, common for all page elements with the AddCanvas() Method.
  3. Make left and top offsets in the main canvas using the CreateMatrix() Method.
  4. Create rectangle path geometry with the CreatePathGeometry() Method.
  5. Create a fill for rectangles by means of the XpsBrush Class.
  6. To add another canvas with a clip to the main canvas call the AddCanvas() Method again.
  7. Create circle geometry for the clip using the XpsPathGeometry Class.
  8. To create a rectangle in this canvas and fill it use the XpsPath Class.
  9. Add another canvas with the AddCanvas() Method then create a rectangle in this canvas and stroke it with the XpsPathGeometry Class.
  10. Save the changed XPS document by means of the XPsDocument.Save() Method.
The canvas clipping
// The path to the documents directory.
System::String dataDir = RunExamples::GetDataDir_WorkingWithCanvas();
// Create new XPS Document
System::SharedPtr<XpsDocument> doc = System::MakeObject<XpsDocument>();
// Create main canvas, common for all page elemnts
System::SharedPtr<XpsCanvas> canvas1 = doc->AddCanvas();
// Make left and top offsets in the main canvas
canvas1->set_RenderTransform(doc->CreateMatrix(1.0f, 0.0f, 0.0f, 1.0f, 20.0f, 10.0f));
// Create rectangle path geometry
System::SharedPtr<XpsPathGeometry> rectGeom = doc->CreatePathGeometry(u"M 0,0 L 500,0 500,300 0,300 Z");
// Create a fill for rectangles
System::SharedPtr<XpsBrush> fill = doc->CreateSolidColorBrush(doc->CreateColor(12, 15, 159));
// Add another canvas with clip to the main canvas
System::SharedPtr<XpsCanvas> canvas2 = canvas1->AddCanvas();
// Create circle geometry for clip
System::SharedPtr<XpsPathGeometry> clipGeom = doc->CreatePathGeometry(u"M250,250 A100,100 0 1 1 250,50 100,100 0 1 1 250,250");
canvas2->set_Clip(clipGeom);
// Create rectangle in this canvas and fill it
System::SharedPtr<XpsPath> rect = canvas2->AddPath(rectGeom);
rect->set_Fill(fill);
// Add the second canvas with stroked rectangle to the main canvas
System::SharedPtr<XpsCanvas> canvas3 = canvas1->AddCanvas();
// Create rectangle in this canvas and stroke it
rect = canvas3->AddPath(rectGeom);
rect->set_Stroke(fill);
rect->set_StrokeThickness(2.0f);
// Save resultant XPS document
doc->Save(dataDir + u"output/" + u"output2.xps");

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.