Using XPS graphic utilities | C++

How to easily produce simple shapes in XPS

The XPS specification defines graphic primitive elements that can be used to compose any complex shapes. Aspose.Page for C++ provides classes encapsulating these elements. However, using them may be a little tedious when you need to produce even relatively simple shapes, such as a circular sector or a segment, or a regular n-gon inscribed in or circumscribed around a circle. Even drawing a circle or an ellipse is not as simple as it could and probably should be. That is why Aspose.Page additionally provides a set of utility methods that will most likely save your time when performing such tasks.

In the following example, we use all available shape utilities. Notice that all of them return an object of XpsPathGeometry, which can then be used to construct XPS paths. And for these paths, you can specify appearance properties - brushes, stroke pattern, opacity, etc.

 1    // ExStart:UsingShapeUtils
 2    // For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-C
 3    // The path to the documents directory.
 4    System::String dataDir = RunExamples::GetDataDir_WorkingWithShapes();
 5    // Create new XPS Document
 6    {
 7        System::SharedPtr<XpsDocument> doc = System::MakeObject<XpsDocument>();
 8        // Clearing resources under 'using' statement
 9        System::Details::DisposeGuard<1> __dispose_guard_0({ doc});
10        // ------------------------------------------
11        
12        try
13        {
14            // Set first page's size.
15            doc->get_Page()->set_Width(650.f);
16            doc->get_Page()->set_Height(240.f);
17            
18            // Draw a circle with center (120, 120) and radius 100.
19            System::SharedPtr<XpsPath> path = doc->CreatePath(doc->get_Utils()->CreateCircle(System::Drawing::PointF(120.f, 120.f), 100.f));
20            path->set_Fill(doc->CreateSolidColorBrush(System::Drawing::Color::get_Green()));
21            doc->Add<System::SharedPtr<XpsPath>>(path);
22            
23            // Inscribe a regular pentagon in the circle.
24            path = doc->CreatePath(doc->get_Utils()->CreateRegularInscribedNGon(5, System::Drawing::PointF(120.f, 120.f), 100.f));
25            path->set_Fill(doc->CreateSolidColorBrush(System::Drawing::Color::get_Red()));
26            doc->Add<System::SharedPtr<XpsPath>>(path);
27            
28            // Circumscribe a regular hexagon around the circle.
29            path = doc->CreatePath(doc->get_Utils()->CreateRegularCircumscribedNGon(6, System::Drawing::PointF(120.f, 120.f), 100.f));
30            path->set_Stroke(doc->CreateSolidColorBrush(System::Drawing::Color::get_Magenta()));
31            path->set_StrokeThickness(3.f);
32            doc->Add<System::SharedPtr<XpsPath>>(path);
33            
34            // Draw a sector of the circle centered at (340, 120), starting at -45 degrees and ending at +45 degrees.
35            path = doc->CreatePath(doc->get_Utils()->CreatePieSlice(System::Drawing::PointF(340.f, 120.f), 100.f, -45.f, 45.f));
36            path->set_Stroke(doc->CreateSolidColorBrush(System::Drawing::Color::get_Red()));
37            path->set_StrokeThickness(5.f);
38            doc->Add<System::SharedPtr<XpsPath>>(path);
39            
40            // Draw a segment of the circle centered at (340, 120), starting at -45 degrees and ending at +45 degrees.
41            path = doc->CreatePath(doc->get_Utils()->CreateCircularSegment(System::Drawing::PointF(340.f, 120.f), 100.f, -45.f, 45.f));
42            path->set_Fill(doc->CreateSolidColorBrush(System::Drawing::Color::get_Black()));
43            doc->Add<System::SharedPtr<XpsPath>>(path);
44            
45            // Draw a rectangle with the top left vertex (530, 20), width 100 units and height 200 units.
46            path = doc->CreatePath(doc->get_Utils()->CreateRectangle(System::Drawing::RectangleF(530.f, 20.f, 100.f, 200.f)));
47            path->set_Stroke(doc->CreateSolidColorBrush(System::Drawing::Color::get_Red()));
48            doc->Add<System::SharedPtr<XpsPath>>(path);
49            
50            // Draw an ellipse with center (580, 120) and radii 50 and 100.
51            path = doc->CreatePath(doc->get_Utils()->CreateEllipse(System::Drawing::PointF(580.f, 120.f), 50.f, 100.f));
52            path->set_Fill(doc->CreateSolidColorBrush(System::Drawing::Color::get_Yellow()));
53            doc->Add<System::SharedPtr<XpsPath>>(path);
54            
55            doc->Save(dataDir + u"UseShapeUtilsXPS_out.xps");
56        }
57        catch(...)
58        {
59            __dispose_guard_0.SetCurrentException(std::current_exception());
60        }
61    }
62    // ExEnd:UsingShapeUtils

We begin with a new XPS document, adjusting then the size of the first page. The first shape we place on the page is a circle (specified by its center and radius) filled with solid green. Next, we inscribe an unfilled red regular pentagon in that circle. Then comes a circumscribed regular hexagon stroked with magenta.

To the right, we first draw a red circular sector (or a “pie slice”) between -45 and +45 degrees, and then a black circular segment with the same parameters over the sector.

The last part of the drawing consists of a red rectangle (specified by the top left vertex and dimensions) and a yellow ellipse (specified by the center and radii), inscribed in the rectangle. Here, we control the inscription “by hand”.

Here is what we are supposed to see in the saved XPS file:

XPS shapes utilities

How to easily add an image on an XPS page

With primitives defined by the XPS specification, adding an image on a page consists of two steps:

But happily, there is a convenient method among XPS graphic utilities of the Aspose.Page API for C++ that can do all (almost) the job for you. It also offers you various fit modes. Let’s look at the example below:

 1    // ExStart:UsingImageUtils
 2    // For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-C
 3    // The path to the documents directory.
 4    System::String dataDir = RunExamples::GetDataDir_WorkingWithImages();
 5    // Create new XPS Document
 6    {
 7        System::SharedPtr<XpsDocument> doc = System::MakeObject<XpsDocument>();
 8        // Clearing resources under 'using' statement
 9        System::Details::DisposeGuard<1> __dispose_guard_0({ doc});
10        // ------------------------------------------
11        
12        try
13        {
14            // Set first page's size.
15            doc->get_Page()->set_Width(540.f);
16            doc->get_Page()->set_Height(220.f);
17            
18            // Draw the image box.
19            System::Drawing::RectangleF imageBox(10.f, 10.f, 200.f, 200.f);
20            System::SharedPtr<XpsPath> path = doc->AddPath(doc->get_Utils()->CreateRectangle(imageBox));
21            path->set_Stroke(doc->CreateSolidColorBrush(System::Drawing::Color::get_Black()));
22            // Add an image to fit width.
23            path = doc->get_Utils()->CreateImage(dataDir + u"R08LN_NN.jpg", imageBox, Aspose::Page::XPS::ImageMode::FitToWidth);
24            // Prevent tiling.
25            (System::ExplicitCast<Aspose::Page::XPS::XpsModel::XpsImageBrush>(path->get_Fill()))->set_TileMode(Aspose::Page::XPS::XpsModel::XpsTileMode::None);
26            doc->Add<System::SharedPtr<XpsPath>>(path);
27            
28            // Add an image to fit width.
29            doc->Add<System::SharedPtr<XpsPath>>(doc->get_Utils()->CreateImage(dataDir + u"R08LN_NN.jpg", System::Drawing::RectangleF(220.f, 10.f, 200.f, 100.f), Aspose::Page::XPS::ImageMode::FitToHeight));
30            
31            // Add an image to fit width.
32            doc->Add<System::SharedPtr<XpsPath>>(doc->get_Utils()->CreateImage(dataDir + u"R08LN_NN.jpg", System::Drawing::RectangleF(430.f, 10.f, 100.f, 200.f), Aspose::Page::XPS::ImageMode::FitToBox));
33            
34            // Save resultant XPS document 
35            doc->Save(dataDir + u"UseImageUtilsXPS_out.xps");
36        }
37        catch(...)
38        {
39            __dispose_guard_0.SetCurrentException(std::current_exception());
40        }
41    }
42    // ExEnd:UsingImageUtils

Again, we start with a new XPS document and adjust the size of the first page. It is worth noting at this point that by default your image being used as a brush will tile across the rectangular area in the same way as if it was used in XpsTileMode.Tile mode. However, in the first part of the example, we demonstrate how to prevent this tiling.

So, we first want the image to appear at (10, 10) and to fit the width of the rectangular box that is 200 units wide and 200 units high. To see the result clearer, we first draw the box itself. Next, we create the image (note that it is actually a filled path). Finally, after getting the path’s fill and casting it to XpsImageBrush, we set the TileMode property to XpsTileMode.None.

To the right, we place the same image and make it fit the height of the image box. Note the tiling.

Finally, we place the same image to the right again and make it fit both the height and width of the image box, which distorts the image.

And here is the result in the saved XPS file:

XPS image utilities

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.