Code brushes and gradients of XPS

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

 

Aspose.Page API Solution for C++ provides you with the capability to manipulate the vector graphics of XPS files. It lets you create various geometric shapes with different colors and textures. Learn also Aspose.Page documentation about how to work with brushes .

To manipulate brushes and gradients of XPS files, we need:

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

  • You can download its latest version directly, just open NuGet package manager, and search for Aspose.Page.Cpp and install. You may also use the following command from the Package Manager Console.

Package Manager Console Command


    PM> Install-Package Aspose.Page

Work with visual brush with C++

The Aspose.Page for C++ API allows you to add a grid to your XPS files using a few lines of code. This can be done for both new and existing documents. The XpsVisualBrush class is utilized to set the properties of the grid, utilizing XpsPathGeometry and XpsCanvas objects. To add a grid to your XPS document, you will need to follow these steps:

  1. Using the XpsDocument Class create a Document object
  2. Create an XpsPathGeometry object with the necessary segments defined.
  3. Utilize the XpsCanvas Class to set the rendering transformation.
  4. Adjust the Fill and Opacity properties as desired.
  5. Save the document by calling the Save() Method.

C++ Code to add a grid to a visual brush

    using Aspose::Page::XPS;
    using Aspose::Page::XPS::XpsModel;
    using System::Drawing;
    // Create geometry for a magenta grid VisualBrush
    System::SharedPtr<XpsPathGeometry> pathGeometry = doc->CreatePathGeometry();
    pathGeometry->AddSegment(doc->CreatePolyLineSegment(System::MakeArray<System::Drawing::PointF>({ System::Drawing::PointF(240.f, 5.f), System::Drawing::PointF(240.f, 310.f), System::Drawing::PointF(0.f, 310.f) })));
    pathGeometry->idx_get(0)->set_StartPoint(System::Drawing::PointF(0.f, 5.f));

    // Create canvas for the magenta grid VisualBrush
    System::SharedPtr<XpsCanvas> visualCanvas = doc->CreateCanvas();

    System::SharedPtr<XpsPath> visualPath = visualCanvas->AddPath(doc->CreatePathGeometry(u"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->set_Fill(doc->CreateSolidColorBrush(doc->CreateColor(1.f, .61f, 0.1f, 0.61f)));

    System::SharedPtr<XpsPath> gridPath = doc->CreatePath(pathGeometry);
    // Create a Visual Brush, it is specified by some XPS fragment (vector graphics and glyphs)
    gridPath->set_Fill(doc->CreateVisualBrush(visualCanvas, System::Drawing::RectangleF(0.f, 0.f, 10.f, 10.f), System::Drawing::RectangleF(0.f, 0.f, 10.f, 10.f)));
    (System::DynamicCast<Aspose::Page::Xps::XpsModel::XpsVisualBrush>(gridPath->get_Fill()))->set_TileMode(Aspose::Page::Xps::XpsModel::XpsTileMode::Tile);

    // Create a new canvas
    System::SharedPtr<XpsCanvas> canvas = doc->AddCanvas();
    canvas->set_RenderTransform(doc->CreateMatrix(1.f, 0.f, 0.f, 1.f, 268.f, 70.f));

    // Add a grid
    canvas->AddPath(gridPath);

    // Create a red transparent rectangle in the middle top
    System::SharedPtr<XpsPath> path = canvas->AddPath(doc->CreatePathGeometry(u"M 30,20 l 258.24,0 0,56.64 -258.24,0 Z"));
    path = canvas->AddPath(doc->CreatePathGeometry(u"M 10,10 L 228,10 228,100 10,100"));
    path->set_Fill(doc->CreateSolidColorBrush(doc->CreateColor(1.0f, 0.0f, 0.0f)));
    path->set_Opacity(0.7f);

    // Save the resultant XPS document
    doc->Save(outDir() + u"AddGrid_out.xps");

Add horizontal gradient with C++.

The Aspose.Page for C++ API allows you to add a grid to your XPS files using a few lines of code. This can be done for both new and existing documents. The XpsVisualBrush class is utilized to set the properties of the grid, utilizing XpsPathGeometry and XpsCanvas objects. To add a grid to your XPS document, you will need to follow these steps:

  1. Using the XpsDocument Class create a Document object
  2. Create an XpsPathGeometry object with the necessary segments defined.
  3. Utilize the XpsCanvas Class to set the rendering transformation.
  4. Adjust the Fill and Opacity properties as desired.

C++ Code to insert horizontal gradient to an XPS file

    // Create new XPS Document
    auto doc = System::MakeObject<XpsDocument>();

    // Initialize List of XpsGradentStop
    System::SharedPtr<System::Collections::Generic::List<System::SharedPtr<XpsGradientStop>>> stops = System::MakeObject<System::Collections::Generic::List<System::SharedPtr<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), 1.f));

    // Create new path by defining geometery in abbreviation form
    System::SharedPtr<XpsPath> path = doc->AddPath(doc->CreatePathGeometry(u"M 10,210 L 228,210 228,300 10,300"));
    path->set_RenderTransform(doc->CreateMatrix(1.f, 0.f, 0.f, 1.f, 20.f, 70.f));
    path->set_Fill(doc->CreateLinearGradientBrush(System::Drawing::PointF(10.f, 0.f), System::Drawing::PointF(228.f, 0.f)));
    (System::DynamicCast<Aspose::Page::Xps::XpsModel::XpsGradientBrush>(path->get_Fill()))->get_GradientStops()->AddRange(stops);

    // Save resultant XPS document
    doc->Save(outDir() + u"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.