Working with Gradient | C++ API Solution
Contents
[
Hide
Show
]Add Gradient in XPS Document
Add Horizontal Gradient
Aspose.Page for C++ lets you add horizontal gradient to an XPS document. The XpsGrandientBrush class is used to specify the XpsGradientStop and XpsPath information to the XpsDocument object which represents the XPS file. The following C++ code shows how to add horizontal gradient information to a document.
- Create an object of XpsDocument class
- Add XpsPath to the XPS document
- Set the rendering information
- Use CreateLinearGradientBrush of XPS document to create gradient brush. Then set this brush to filling
- Use set_GradientStops to set XpsGradientStop’s for the brush
- Save the document
1For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-C
2// Create new XPS Document
3auto doc = System::MakeObject<XpsDocument>();
4// Initialize List of XpsGradentStop
5System::SharedPtr<System::Collections::Generic::List<System::SharedPtr<XpsGradientStop>>> stops = System::MakeObject<System::Collections::Generic::List<System::SharedPtr<XpsGradientStop>>>();
6stops->Add(doc->CreateGradientStop(doc->CreateColor(255, 244, 253, 225), 0.0673828f));
7stops->Add(doc->CreateGradientStop(doc->CreateColor(255, 251, 240, 23), 0.314453f));
8stops->Add(doc->CreateGradientStop(doc->CreateColor(255, 252, 209, 0), 0.482422f));
9stops->Add(doc->CreateGradientStop(doc->CreateColor(255, 241, 254, 161), 0.634766f));
10stops->Add(doc->CreateGradientStop(doc->CreateColor(255, 53, 253, 255), 0.915039f));
11stops->Add(doc->CreateGradientStop(doc->CreateColor(255, 12, 91, 248), 1.f));
12// Create new path by defining geometery in abbreviation form
13System::SharedPtr<XpsPath> path = doc->AddPath(doc->CreatePathGeometry(u"M 10,210 L 228,210 228,300 10,300"));
14path->set_RenderTransform(doc->CreateMatrix(1.f, 0.f, 0.f, 1.f, 20.f, 70.f));
15path->set_Fill(doc->CreateLinearGradientBrush(System::Drawing::PointF(10.f, 0.f), System::Drawing::PointF(228.f, 0.f)));
16(System::DynamicCast<Aspose::Page::Xps::XpsModel::XpsGradientBrush>(path->get_Fill()))->get_GradientStops()->AddRange(stops);
17// Save resultant XPS document
18doc->Save(outDir() + u"AddHorizontalGradient_out.xps");
Add Vertical Gradient
Just as you can add the Horizontal gradient, you can use Aspose.Page for C++ to add vertical gradient information to the XPS document. The following C++ code sample shows how to add a verticle gradient to an XPS document.
1For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-C
2// Create new XPS Document
3auto doc = System::MakeObject<XpsDocument>();
4// Initialize List of XpsGradentStop
5auto stops = System::MakeObject<System::Collections::Generic::List<System::SharedPtr<XpsGradientStop>>>();
6stops->Add(doc->CreateGradientStop(doc->CreateColor(253, 255, 12, 0), 0.f));
7stops->Add(doc->CreateGradientStop(doc->CreateColor(252, 255, 154, 0), 0.359375f));
8stops->Add(doc->CreateGradientStop(doc->CreateColor(252, 255, 56, 0), 0.424805f));
9stops->Add(doc->CreateGradientStop(doc->CreateColor(253, 255, 229, 0), 0.879883f));
10stops->Add(doc->CreateGradientStop(doc->CreateColor(252, 255, 255, 234), 1.f));
11// Create new path by defining geometery in abbreviation form
12System::SharedPtr<XpsPath> path = doc->AddPath(doc->CreatePathGeometry(u"M 10,110 L 228,110 228,200 10,200"));
13path->set_RenderTransform(doc->CreateMatrix(1.f, 0.f, 0.f, 1.f, 20.f, 70.f));
14path->set_Fill(doc->CreateLinearGradientBrush(System::Drawing::PointF(10.f, 110.f), System::Drawing::PointF(10.f, 200.f)));
15(System::DynamicCast<Aspose::Page::Xps::XpsModel::XpsGradientBrush>(path->get_Fill()))->get_GradientStops()->AddRange(stops);
16// Save resultant XPS document
17doc->Save(outDir() + u"AddVerticalGradient_out.xps");
Add Linear Gradient
You can also add linear gradient to an XPS document using Aspose.Page for C++ as shown in the following code sample.
1For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-C
2// Create new XPS Document
3auto doc = System::MakeObject<XpsDocument>();
4// Initialize List of XpsGradentStop
5System::SharedPtr<System::Collections::Generic::List<System::SharedPtr<XpsGradientStop>>> stops = System::MakeObject<System::Collections::Generic::List<System::SharedPtr<XpsGradientStop>>>();
6// Add Colors to Gradient
7stops->Add(doc->CreateGradientStop(doc->CreateColor(0, 142, 4), 0.f));
8stops->Add(doc->CreateGradientStop(doc->CreateColor(255, 202, 0), 0.144531f));
9stops->Add(doc->CreateGradientStop(doc->CreateColor(255, 250, 0), 0.264648f));
10stops->Add(doc->CreateGradientStop(doc->CreateColor(255, 0, 0), 0.414063f));
11stops->Add(doc->CreateGradientStop(doc->CreateColor(233, 0, 255), 0.544922f));
12stops->Add(doc->CreateGradientStop(doc->CreateColor(107, 27, 190), 0.694336f));
13stops->Add(doc->CreateGradientStop(doc->CreateColor(63, 0, 255), 0.844727f));
14stops->Add(doc->CreateGradientStop(doc->CreateColor(0, 199, 80), 1.f));
15// Create new path by defining geometery in abbreviation form
16System::SharedPtr<XpsPath> path = doc->AddPath(doc->CreatePathGeometry(u"M 10,10 L 228,10 228,100 10,100"));
17path->set_RenderTransform(doc->CreateMatrix(1.f, 0.f, 0.f, 1.f, 20.f, 70.f));
18path->set_Fill(doc->CreateLinearGradientBrush(System::Drawing::PointF(10.f, 10.f), System::Drawing::PointF(228.f, 100.f)));
19(System::DynamicCast<Aspose::Page::Xps::XpsModel::XpsGradientBrush>(path->get_Fill()))->get_GradientStops()->AddRange(stops);
20// Save resultant XPS document
21doc->Save(outDir() + u"AddLinearGradient_out.xps");