Work with canvases within XPS

Clip and transform canvases in XPS files using the C++ API

 

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, which provides 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 documents is one of the features offered by Aspose.Page for C++. This solution works with different page description languages, XPS in particular.

To transform canvases of an XPS file, follow the guide below:

  1. Create an XPS file using the XpsDocument class.
  2. Create the main canvas, common to all page elements, using 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 using 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, use the Translate() method.
  9. To scale canvas 4, call the Scale() method.
  10. To rotate canvas 5 around a point by 45 degrees; the RotateAround() method comes 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 an XPS file, follow the guide below:

  1. Create or open an XPS file using the XpsDocument class.
  2. Create the main canvas, common to all page elements, using 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 using 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 using 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 (XML Paper Specification) is Microsoft’s open‑source alternative to PDF. It uses XML/HTML markup to describe page layout, fonts, and images, ensuring consistent rendering across Windows platforms and other operating systems.