Working with Visual Brush | C++

Contents
[ Hide Show ]

Add Grid using Visual Brush

Aspose.Page for C++ API lets you add a grid to the XPS document. Grid can be added to new as well as existing XPS documents using simple lines of code in C++ applications. This can be achieved using XpsVisualBrush class that is used to specify the properties of the grid with XpsPathGeometry and XpsCanvas objects. To add a grid to the XPS document, use the following steps.

  1. Create a Document object using the XpsDocument class
  2. Create an XpsPathGeometry object with defined required Segments
  3. Create an object of XpsCanvas and set rendering Transformation
  4. Set Fill and Opacity properties
  5. Save the document using the Save method
 1For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-C
 2auto doc = System::MakeObject<XpsDocument>();
 3// Geometry for magenta grid VisualBrush
 4System::SharedPtr<XpsPathGeometry> pathGeometry = doc->CreatePathGeometry();
 5pathGeometry->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) })));
 6pathGeometry->idx_get(0)->set_StartPoint(System::Drawing::PointF(0.f, 5.f));
 7
 8// Canvas for magenta grid VisualBrush
 9System::SharedPtr<XpsCanvas> visualCanvas = doc->CreateCanvas();
10
11System::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"));
12visualPath->set_Fill(doc->CreateSolidColorBrush(doc->CreateColor(1.f, .61f, 0.1f, 0.61f)));
13
14System::SharedPtr<XpsPath> gridPath = doc->CreatePath(pathGeometry);
15//Create Visual Brush, it is specified by some XPS fragment (vector graphics and glyphs)
16gridPath->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)));
17(System::DynamicCast<Aspose::Page::Xps::XpsModel::XpsVisualBrush>(gridPath->get_Fill()))->set_TileMode(Aspose::Page::Xps::XpsModel::XpsTileMode::Tile);
18// New canvas
19System::SharedPtr<XpsCanvas> canvas = doc->AddCanvas();
20canvas->set_RenderTransform(doc->CreateMatrix(1.f, 0.f, 0.f, 1.f, 268.f, 70.f));
21// Add grid
22canvas->AddPath(gridPath);
23// Red transparent rectangle in the middle top
24System::SharedPtr<XpsPath> path = canvas->AddPath(doc->CreatePathGeometry(u"M 30,20 l 258.24,0 0,56.64 -258.24,0 Z"));
25path = canvas->AddPath(doc->CreatePathGeometry(u"M 10,10 L 228,10 228,100 10,100"));
26path->set_Fill(doc->CreateSolidColorBrush(doc->CreateColor(1.0f, 0.0f, 0.0f)));
27path->set_Opacity(0.7f);
28// Save resultant XPS document
29doc->Save(outDir() + u"AddGrid_out.xps");
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.