Working with brushes and gradients

C# .NET API solution to work with brushes and gradients of XPS files.

 

Aspose.Page for .NET API solution lets you manipulate vector graphics of XPS files. You can create various geometric shapes of different colors and textures. On this page will be described a few examples of how to add different color spaces, and gradients, and how to work with different brushes. Working analogically you will be able to create any geometric figure you need of any color.

To manipulate brushes and gradients of XPS files, 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 apply color spaces with C# .NET.

  1. Set the path to the documents directory.
  2. Create an XPS file using the XpsDocument Class .
  3. To create different solid color filled rectangles use methods of the XpsPath Class.
  4. Save the changed XPS document using the XPsDocument.Save() Method.

C# Code to set color spaces of an XPS file

    using Aspose.Page.XPS;
    using Aspose.Page.XPS.XpsModel;
    using System.Drawing;
    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_WorkingWithShapes();
            
    // Create a new XPS Document
    XpsDocument doc = new XpsDocument();

    // ARGB solid color filled rectangle
    XpsPath rect1 = doc.AddPath(doc.CreatePathGeometry("M 20,10 L 220,10 220,100 20,100 Z"));
    rect1.Fill = doc.CreateSolidColorBrush(doc.CreateColor(Color.FromArgb(222, 12, 15, 159)));

    // ARGB solid color filled rectangle, in another way
    XpsPath rect2 = doc.AddPath(doc.CreatePathGeometry("M 20,210 L 220,210 220,300 20,300 Z"));
    rect2.Fill = doc.CreateSolidColorBrush(doc.CreateColor(222, 12, 15, 159));

    // sRGB solid color filled rectangle
    XpsPath rect3 = doc.AddPath(doc.CreatePathGeometry("M 20,410 L 220,410 220,500 20,500 Z"));
    rect3.Fill = doc.CreateSolidColorBrush(doc.CreateColor(12, 15, 159));

    // scRGB solid color filled rectangle
    XpsPath rect4 = doc.AddPath(doc.CreatePathGeometry("M 20,610 L 220,610 220,700 20,700 Z"));
    rect4.Fill = doc.CreateSolidColorBrush(doc.CreateColor(0.08706f, 0.04706f, 0.05882f, 0.62353f));

    // CMYK (blue) solid color filled rectangle
    XpsPath rect5 = doc.AddPath(doc.CreatePathGeometry("M 20,810 L 220,810 220,900 20,900 Z"));
    rect5.Fill = doc.CreateSolidColorBrush(
        doc.CreateColor(dataDir + "uswebuncoated.icc", 1.0f, 1.000f, 0.000f, 0.000f, 0.000f));
            
    // Save the resultant XPS document
    doc.Save(dataDir + "ApplyDifferentColorSpaces_out.xps");
The next code snippet shows how to use visual brush of an XPS file within the Aspose.Page for .NET Api Solution.

Steps to work with visual brush with C#.

  1. Set the path to the documents directory.
  2. Create an XPS file using the XpsDocument Class.
  3. Create geometry for magenta grid Visual Brush using the XpsPathGeometry Class.
  4. To set the canvas for the magenta grid Visual Brush use the CreateCanvas() Method.
  5. To create Visual Brush use the CreateVisualBrush() Method.
  6. Save the changed XPS document by means of XPsDocument.Save() Method.

C# Code to manipulate visual brush of an XPS file

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

    XpsDocument doc = new XpsDocument();
    // Geometry for the magenta grid VisualBrush
    XpsPathGeometry pathGeometry = doc.CreatePathGeometry();
    pathGeometry.AddSegment(doc.CreatePolyLineSegment(
        new PointF[] { new PointF(240f, 5f), new PointF(240f, 310f), new PointF(0f, 310f) }));
    pathGeometry[0].StartPoint = new PointF(0f, 5f);

    // Canvas for the magenta grid VisualBrush
    XpsCanvas visualCanvas = doc.CreateCanvas();

    XpsPath visualPath = visualCanvas.AddPath(
        doc.CreatePathGeometry("M 0,4 L 4,4 4,0 6,0 6,4 10,4 10,6 6,6 6,10 4,10 4,6 0,6 Z"));
    visualPath.Fill = doc.CreateSolidColorBrush(doc.CreateColor(1f, .61f, 0.1f, 0.61f));

    XpsPath gridPath = doc.CreatePath(pathGeometry);
    // Create the Visual Brush, it is specified by some XPS fragment (vector graphics and glyphs)
    gridPath.Fill = doc.CreateVisualBrush(visualCanvas,
        new RectangleF(0f, 0f, 10f, 10f), new RectangleF(0f, 0f, 10f, 10f));
    ((XpsVisualBrush)gridPath.Fill).TileMode = XpsTileMode.Tile;
    // New canvas
    XpsCanvas canvas = doc.AddCanvas();
    canvas.RenderTransform = doc.CreateMatrix(1f, 0f, 0f, 1f, 268f, 70f);
    // Add a grid
    canvas.AddPath(pathGeometry);
    // Red transparent rectangle in the middle top
    XpsPath path = canvas.AddPath(doc.CreatePathGeometry("M 30,20 l 258.24,0 0,56.64 -258.24,0 Z"));
    path = canvas.AddPath(doc.CreatePathGeometry("M 10,10 L 228,10 228,100 10,100"));
    path.Fill = doc.CreateSolidColorBrush(doc.CreateColor(1.0f, 0.0f, 0.0f));
    path.Opacity = 0.7f;
    // Save the resultant XPS document
    doc.Save(dataDir + "AddGrid_out.xps");

Steps to add horizontal gradient with C#.

  1. Set the path to the documents directory.
  2. Create an XPS file using the XpsDocument Class.
  3. Initialize a List of XpsGradentStop using the CreateGradientStop() and CreateColor() Methods.
  4. Create new a path by defining geometry in an abbreviation form using methods of the XpsPath Class.
  5. Save the changed XPS document by means of the XPsDocument.Save() Method.

C# Code to insert horizontal gradient to an XPS file

    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_WorkingWithGradient();
    // Create a new XPS Document
    XpsDocument doc = new XpsDocument();

    // Initialize the List of XpsGradentStop
    List<XpsGradientStop> stops = new List<XpsGradientStop>();
    stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 244, 253, 225), 0.0673828f));
    stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 251, 240, 23), 0.314453f));
    stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 252, 209, 0), 0.482422f));
    stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 241, 254, 161), 0.634766f));
    stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 53, 253, 255), 0.915039f));
    stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 12, 91, 248), 1f));

    // Create a new path by defining the geometery in the abbreviation form
    XpsPath path = doc.AddPath(doc.CreatePathGeometry("M 10,210 L 228,210 228,300 10,300"));
    path.RenderTransform = doc.CreateMatrix(1f, 0f, 0f, 1f, 20f, 70f);
    path.Fill = doc.CreateLinearGradientBrush(new PointF(10f, 0f), new PointF(228f, 0f));
    ((XpsGradientBrush)path.Fill).GradientStops.AddRange(stops);

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



FAQ

1. How can I add a gradient to an XPS file?

Set the path to the documents directory. To add gradient use the CreateColor() and CreateGradientStop() Methods.

2. How to work with a visual brush in an XPS file?

Set the path to the documents directory. To create visual brush use the CreateVisualBrush() Method.

3. How to open an XPS file?

Use Aspose.Page API Solution to open XPS file programmatically or by means of cross-platform XPS Viewer .

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.