Using XPS graphic utilities | Java

How to easily produce basic shapes in XPS

The XPS specification defines graphic primitive elements that can be used to compose any complex shapes. Aspose.Page for Java provides classes encapsulating these elements. However, creating even relatively simple shapes, such as a circular sector or segment, or a regular n-gon inscribed in or circumscribed around a circle, can be a bit tedious. Even drawing a circle or an ellipse is not as straightforward as it could be. That is why Aspose.Page also offers a set of utility methods to help save time when working on these tasks.

In the example below, we use all available shape utilities. It is important to note that each of these utility methods returns an object of XpsPathGeometry, which can then be used to create XPS paths. And for these paths, you can specify appearance properties - brushes, stroke pattern, opacity, etc.

 1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
 2// The path to the documents directory.
 3String dataDir = Utils.getDataDir();
 4// Create new XPS Document
 5final XpsDocument doc = new XpsDocument();
 6try {
 7    // Set first page's size.
 8    doc.getPage().setWidth(650f);
 9    doc.getPage().setHeight(240f);
10
11    // Draw a circle with center (120, 120) and radius 100.
12    XpsPath path = doc.createPath(doc.getUtils().createCircle(new Point2D.Float(120f, 120f), 100f));
13    path.setFill(doc.createSolidColorBrush(Color.GREEN));
14    doc.add(path);
15
16    // Inscribe a regular pentagon in the circle.
17    path = doc.createPath(doc.getUtils().createRegularInscribedNGon(5, new Point2D.Float(120f, 120f), 100f));
18    path.setFill(doc.createSolidColorBrush(Color.RED));
19    doc.add(path);
20
21    // Circumscribe a regular hexagon around the circle.
22    path = doc.createPath(doc.getUtils().createRegularCircumscribedNGon(6, new Point2D.Float(120f, 120f), 100f));
23    path.setStroke(doc.createSolidColorBrush(Color.MAGENTA));
24    path.setStrokeThickness(3f);
25    doc.add(path);
26
27    // Draw a sector of the circle centered at (340, 120), starting at -45 degrees and ending at +45 degrees.
28    path = doc.createPath(doc.getUtils().createPieSlice(new Point2D.Float(340f, 120f), 100f, -45f, 45f));
29    path.setStroke(doc.createSolidColorBrush(Color.RED));
30    path.setStrokeThickness(5f);
31    doc.add(path);
32
33    // Draw a segment of the circle centered at (340, 120), starting at -45 degrees and ending at +45 degrees.
34    path = doc.createPath(doc.getUtils().createCircularSegment(new Point2D.Float(340f, 120f), 100f, -45f, 45f));
35    path.setFill(doc.createSolidColorBrush(Color.BLACK));
36    doc.add(path);
37
38    // Draw a rectangle with the top left vertex (530, 20), width 100 units and height 200 units.
39    path = doc.createPath(doc.getUtils().createRectangle(new Rectangle2D.Float(530f, 20f, 100f, 200f)));
40    path.setStroke(doc.createSolidColorBrush(Color.RED));
41    doc.add(path);
42
43    // Draw an ellipse with center (580, 120) and radii 50 and 100.
44    path = doc.createPath(doc.getUtils().createEllipse(new Point2D.Float(580f, 120f), 50f, 100f));
45    path.setFill(doc.createSolidColorBrush(Color.YELLOW));
46    doc.add(path);
47
48    doc.save(dataDir + "UseShapeUtilsXPS_out.xps");
49} finally {
50  if (doc != null)
51    doc.close();
52}

We start by creating a new XPS document and then adjusting the size of the first page. The first shape we add to the page is a solid green circle specified by its center and radius. Next, we draw an unfilled red regular pentagon inscribed in that circle. Then comes a regular hexagon stroked with magenta and circumscribed around the circle.

To the right, we draw a red circular sector (or a “pie slice”) between -45 and +45 degrees, followed by a black circular segment with the same parameters on top of the sector.

The last part of the drawing includes a red rectangle (specified by its top left corner and dimensions) and a yellow ellipse (specified by its center and radii) inscribed in the rectangle. In this case, we handle the inscription “by hand”.

Here is the intended output in the saved XPS file:

XPS shapes utilities

How to easily place an image on an XPS page

When using primitives defined by the XPS specification, placing an image on a page involves two steps:

But fortunately, there is a convenient method among XPS graphic utilities in the Aspose.Page API for Java that can handle most of the work for you. It also offers you common fit modes. Let’s look at the following example:

 1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
 2// The path to the documents directory.
 3String dataDir = Utils.getDataDir();
 4// Create new XPS Document
 5final XpsDocument doc = new XpsDocument();
 6try {
 7    // Set first page's size.
 8    doc.getPage().setWidth(540f);
 9    doc.getPage().setHeight(220f);
10
11    // Draw the image box.
12    Rectangle2D imageBox = new Rectangle2D.Float(10f, 10f, 200f, 200f);
13    XpsPath path = doc.addPath(doc.getUtils().createRectangle(imageBox));
14    path.setStroke(doc.createSolidColorBrush(Color.BLACK));
15    // Add an image to fit width.
16    path = doc.getUtils().createImage(dataDir + "R08LN_NN.jpg", imageBox, ImageMode.FitToWidth);
17    // Prevent tiling.
18    ((XpsImageBrush)path.getFill()).setTileMode(XpsTileMode.None);
19    doc.add(path);
20
21    // Add an image to fit width.
22    doc.add(doc.getUtils().createImage(
23        dataDir + "R08LN_NN.jpg", new Rectangle2D.Float(220f, 10f, 200f, 100f), ImageMode.FitToHeight));
24
25    // Add an image to fit width.
26    doc.add(doc.getUtils().createImage(
27        dataDir + "R08LN_NN.jpg", new Rectangle2D.Float(430f, 10f, 100f, 200f), ImageMode.FitToBox));
28
29    // Save resultant XPS document 
30    doc.save(dataDir + "UseImageUtilsXPS_out.xps");
31} finally {
32  if (doc != null)
33    doc.close();
34}

Again, we begin with a new XPS document and resize the first page. It is worth mentioning here that by default your image being used as a brush will tile across the image box in the same way as if it was used in XpsTileMode.Tile mode. However, in the first part of the example, we show how to prevent this tiling.

So, we first want the image to be displayed at coordinates (10, 10) and to fit the width of the rectangular box, which is 200 units wide and 200 units high. To better visualize the result, we first draw the box itself. Then, we proceed to create the image (which is essentially 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 fitting the height of the image box. Notice the tiling effect.

Finally, we place the same image to the right once more and adjust it to fit both the height and width of the image box, resulting in some distortion of the image.

And here is the result in the saved XPS file:

XPS image utilities

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.