Insert vector graphics shapes into XPS files

C# .NET API solution to work with vector graphics of XPS files

 

As any other Page Description Language format, XPS allows including vector graphics. The solution lets you manipulate vector graphics of XPS files. The functionality is rich so here will be described just a few examples of how to add ovals and rectangles shapes to files. Here you will also see how to work with the colors of the shapes. Working analogically you will be able to create any geometric figure you need.

To work with vector graphics shapes of XPS, we need:

  • Aspose.Page for .NET API which is a feature-rich, powerful and easy-to-use document manipulation and conversion API for the C# platform.

  • Open the NuGet package manager, and search for Aspose.Page and install. You may also use the following command from the Package Manager Console.

Package Manager Console Command


    PM> Install-Package Aspose.Page

Steps to add a rectangle with C#.

  1. Set the path to the documents directory.
  2. Create an XPS file using the XpsDocument Class .
  3. To create CMYK (blue) solid color stroked rectangle in the lower left use methods of the XpsPath Class.
  4. Save the changed XPS document using the XPsDocument.Save Method.

C# Code to insert a rectangle to an XPS file

    using Aspose.Page.XPS;
    using Aspose.Page.XPS.XpsModel;
    using System.Drawing;
    using System.Collections.Generic;
    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_WorkingWithShapes();

    // Create a new XPS Document
    XpsDocument doc = new XpsDocument();

    // CMYK (blue) solid color stroked rectangle in the lower left
    XpsPath path = doc.AddPath(doc.CreatePathGeometry("M 20,10 L 220,10 220,100 20,100 Z"));
    path.Stroke = doc.CreateSolidColorBrush(
        doc.CreateColor(dataDir + "uswebuncoated.icc", 1.0f, 1.000f, 0.000f, 0.000f, 0.000f));
    path.StrokeThickness = 12f;

    // Save the resultant XPS document
     doc.Save(dataDir + "AddRectangle_out.xps");
The next code snippet shows how to put an ellipse into an XPS files within the Aspose.Page for .NET Api Solution.

Steps to add an ellipse with C#.

  1. Set the path to the documents directory.
  2. Open a stream of the XPS file.
  3. Create an XPS file using the XpsDocument Class.
  4. To create the radial gradient stroked ellipse in the lower left use methods of the XpsPath Class.
  5. Save the changed XPS document by means of the XPsDocument.Save() Method.

C# Code to insert an ellipse to an XPS file

    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_WorkingWithShapes();

    // Create a new XPS Document
    XpsDocument doc = new XpsDocument();

    // Radial gradient stroked ellipse in the lower left
    List<XpsGradientStop> stops = new List<XpsGradientStop>();
    stops.Add(doc.CreateGradientStop(doc.CreateColor(0, 0, 255), 0f));
    stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 0, 0), .25f));
    stops.Add(doc.CreateGradientStop(doc.CreateColor(0, 255, 0), .5f));
    stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 255, 0), .75f));
    stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 0, 0), 1f));

    XpsPath path = doc.AddPath(doc.CreatePathGeometry("M 20,250 A 100,50 0 1 1 220,250 100,50 0 1 1 20,250"));
    path.Stroke = doc.CreateRadialGradientBrush(new PointF(575f, 125f), new PointF(575f, 100f), 75f, 50f);
    ((XpsGradientBrush)path.Stroke).SpreadMethod = XpsSpreadMethod.Reflect;
    ((XpsGradientBrush)path.Stroke).GradientStops.AddRange(stops);
    stops.Clear();
    path.StrokeThickness = 12f;

    // Save the resultant XPS document
    doc.Save(dataDir + "AddEllipse_out.xps");



FAQ

1. How to draw graphics in an XPS?

To manipulate vector graphic objects within XPS use the corresponding Classes of the Aspose.Page.XPS.XpsModel namespace.

2. Can I change the color of the graphics within XPS?

Yes, you can manage colors, gradients, brushes, canvases, etc.

3. How can I add geometric figures to an XPS file?

Open a stream of the XPS file. To add the needed figure use methods of the XpsPath Class.

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.