Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created January 18, 2021 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/fcfc294209efcb646f0a0de5ddfff050 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/fcfc294209efcb646f0a0de5ddfff050 to your computer and use it in GitHub Desktop.
Insert Gradient, Grid, or Transparent Objects in XPS File with 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");
// 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");
// 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");
// 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");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment