Add Gradient Grid Transparency XPS

XPS files can be used to display a lot of visual information. They support text, images, object transparency, grids, gradients, and many other features. You can insert gradient, grid, and transparent objects in XPS files as per your requirements. In this article, we will be exploring these features in detail, along with different examples and use cases. Let us walk through the following topics:

Installing XPS API for C++

You can quickly and easily configure Aspose.Page for C++ API in your programming applications for manipulating XPS, PS, EPS, and other supported file formats. Simply download the DLL file from official Downloads, or install it from NuGet gallery with the command below:

PM> Install-Package Aspose.Page.Cpp 

Adding Gradient in XPS file using C++

Sometimes you need to add gradient in XPS files. You can manipulate the XPS file as the API lets you add linear, horizontal as well as vertical gradient. Let us add the following gradients in a XPS file:

Add Vertical Gradient in XPS file with C++

You can easily add vertical gradient in XPS files with a few simple API calls. Below are the steps for adding the vertical gradient:

  1. Create a new XPS Document
  2. Initialize List of XpsGradientStop
  3. Create a new path by defining the geometry
  4. Save resultant XPS document

The code below is based on these steps, which demonstrates how to add a vertical gradient in an XPS file using C++:

// Create new XPS Document
auto doc = System::MakeObject<XpsDocument>();
// Initialize List of XpsGradientStop
auto stops = System::MakeObject<System::Collections::Generic::List<System::SharedPtr<XpsGradientStop>>>();
stops->Add(doc->CreateGradientStop(doc->CreateColor(253, 255, 12, 0), 0.f));
stops->Add(doc->CreateGradientStop(doc->CreateColor(252, 255, 154, 0), 0.359375f));
stops->Add(doc->CreateGradientStop(doc->CreateColor(252, 255, 56, 0), 0.424805f));
stops->Add(doc->CreateGradientStop(doc->CreateColor(253, 255, 229, 0), 0.879883f));
stops->Add(doc->CreateGradientStop(doc->CreateColor(252, 255, 255, 234), 1.f));
// Create new path by defining geometery in abbreviation form
System::SharedPtr<XpsPath> path = doc->AddPath(doc->CreatePathGeometry(u"M 10,110 L 228,110 228,200 10,200"));
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, 110.f), System::Drawing::PointF(10.f, 200.f)));
(System::DynamicCast<Aspose::Page::Xps::XpsModel::XpsGradientBrush>(path->get_Fill()))->get_GradientStops()->AddRange(stops);
// Save resultant XPS document
doc->Save(RunExamples::outDir() + u"AddVerticalGradient_out.xps");

Add Horizontal Gradient in XPS using C++

Another possible variation of gradient in XPS files is the horizontal gradient. The approach of adding horizontal gradient is bit related to the use case we have discussed above. The following are the steps that you need to follow for adding horizontal gradient:

  1. Create new XPS Document
  2. Specify gradient stops using XpsGradientStop
  3. Create path with geometry
  4. Save output XPS document

The following code snippet elaborates how to add horizontal gradient in XPS files using C++:

// Create new XPS Document
auto doc = System::MakeObject<XpsDocument>();
// Initialize List of XpsGradientStop
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(RunExamples::outDir() + u"AddHorizontalGradient_out.xps");

Insert Grid in XPS file with C++

Aspose.Page for C++ API lets you render grids in the XPS files with a lot of properties to control the rendering. Let us consider the example of inserting grid in XPS file in your C++ based applications. You can work with this feature by considering the steps below:

  1. Initialize XpsDocument class object
  2. Specify Geometry for the grid VisualBrush
  3. Create Canvas for magenta grid VisualBrush
  4. Create Visual brush and add grid
  5. Save output XPS file

The code below is based on these steps which explains how to insert grid in XPS files using C++:

auto doc = System::MakeObject<XpsDocument>();
// Geometry for 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));
// Canvas for 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 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);
// 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 grid
canvas->AddPath(gridPath);
// 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 resultant XPS document
doc->Save(RunExamples::outDir() + u"AddGrid_out.xps");
view raw Grid.cpp hosted with ❤ by GitHub

Insert Transparent Object in XPS file using C++

Another exciting feature for working with XPS files is the support for transparency and opacity. You might need to add transparent objects on the XPS file under different scenarios. This can be achieved with the help of the following steps:

  1. Create new XPS Document
  2. Create path with closed rectangle geometry
  3. Add geometry for different paths
  4. Save output XPS document

These steps are followed by the following code, that demonstrates how to add transparent object in XPS file with C++ programming language:

// Create new XPS Document
auto doc = System::MakeObject<XpsDocument>();
// Just to demonstrate transparency
doc->AddPath(doc->CreatePathGeometry(u"M120,0 H400 v1000 H120"))->set_Fill(doc->CreateSolidColorBrush(System::Drawing::Color::get_Gray()));
doc->AddPath(doc->CreatePathGeometry(u"M300,120 h600 V420 h-600"))->set_Fill(doc->CreateSolidColorBrush(System::Drawing::Color::get_Gray()));
// Create path with closed rectangle geometry
System::SharedPtr<XpsPath> path1 = doc->CreatePath(doc->CreatePathGeometry(u"M20,20 h200 v200 h-200 z"));
// Set blue solid brush to fill path1
path1->set_Fill(doc->CreateSolidColorBrush(System::Drawing::Color::get_Blue()));
// Add it to the current page
System::SharedPtr<XpsPath> path2 = doc->Add(path1);
// path1 and path2 are the same as soon as path1 hasn't been placed inside any other element
// (which means that path1 had no parent element).
// Because of that rectangle's color on the page effectively turns to green
path2->set_Fill(doc->CreateSolidColorBrush(System::Drawing::Color::get_Green()));
// Now add path2 once again. Now path2 has parent. So path3 won't be the same as path2.
// Thus a new rectangle is painted on the page ...
System::SharedPtr<XpsPath> path3 = doc->Add(path2);
// ... and we shift it 300 units lower ...
path3->set_RenderTransform(doc->CreateMatrix(1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 300.0f));
// ... and set red solid brush to fill it
path3->set_Fill(doc->CreateSolidColorBrush(System::Drawing::Color::get_Red()));
// Create new path4 with path2's geometry ...
System::SharedPtr<XpsPath> path4 = doc->AddPath(path2->get_Data());
// ... shift it 300 units to the right ...
path4->set_RenderTransform(doc->CreateMatrix(1.0f, 0.0f, 0.0f, 1.0f, 300.0f, 0.0f));
// ... and set blue solid fill
path4->set_Fill(doc->CreateSolidColorBrush(System::Drawing::Color::get_Blue()));
// Add path4 once again.
System::SharedPtr<XpsPath> path5 = doc->Add(path4);
// path4 and path5 are not the same again ...
// (move path5 300 units lower)
path5->set_RenderTransform(path5->get_RenderTransform()->Clone());
// to disconnect RenderTransform value from path4 (see next comment about Fill property)
path5->get_RenderTransform()->Translate(0.0f, 300.0f);
// ... but if we set the opacity of Fill property, it will take effect on both path5 and path4
// because brush is a complex property value which remains the same for path5 and path4
path5->get_Fill()->set_Opacity(0.8f);
// Create new path6 with path2's geometry ...
System::SharedPtr<XpsPath> path6 = doc->AddPath(path2->get_Data());
// ... shift it 600 units to the right ...
path6->set_RenderTransform(doc->CreateMatrix(1.0f, 0.0f, 0.0f, 1.0f, 600.0f, 0.0f));
// ... and set yellow solid fill
path6->set_Fill(doc->CreateSolidColorBrush(System::Drawing::Color::get_Yellow()));
// Now add path6's clone ...
System::SharedPtr<XpsPath> path7 = doc->Add(path6->Clone());
// (move path5 300 units lower)
path7->set_RenderTransform(path7->get_RenderTransform()->Clone());
path7->get_RenderTransform()->Translate(0.0f, 300.0f);
// ... and set opacity for path7
path7->get_Fill()->set_Opacity(0.8f);
// Now opacity effects independantly as soon as property values are cloned along with the element
// The following code block is equivalent to the previous one.
// Add path6 itself. path6 and path7 are not the same. Although their Fill property values are the same
//XpsPath path7 = doc.Add(path6);
//path7.RenderTransform = path7.RenderTransform.Clone();
//path7.RenderTransform.Translate(0, 300);
// To "disconnect" path7's Fill property from path6's Fill property reassign it to its clone (or path6's Fill clone)
//path7.Fill = ((XpsSolidColorBrush)path7.Fill).Clone();
//path7.Fill.Opacity = 0.8f;
// Save resultant XPS document
doc->Save(RunExamples::outDir() + u"WorkingWithTransparency_out.xps");

Conclusion

In a nutshell, we have explored a number of features to work with XPS files. You can add a gradient, linear, horizontal, or vertical, as well as grids or transparent objects in XPS files. Moreover, you can take a look at several other features by downloading the Examples project. In case of any ambiguity, please feel free to write to us at the Free Support Forums. We will be honored to guide you!

See Also