Working with Images | C++ API Solution
Contents
[
Hide
Show
]Add Image inside XPS Documents
Add Image
Aspose.Page for C++ API lets you create/read XPS documents and add image to it. This can be done by creating XpsMatrix and XpsImageBrush to add the image to the XPS file. In order to add an image to XPS document, use the following steps.
- Create an object of XpsDocument class
- Create XpsPath object with defined Path Geometry
- Set Render Transformation for the path using set_RenderTransform
- Fill path with creared image brush using set_Fill
- Save the document to disc using the Save method
1For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-C
2// Create new XPS Document
3System::SharedPtr<XpsDocument> doc = System::MakeObject<XpsDocument>();
4// Add Image
5System::SharedPtr<XpsPath> path = doc->AddPath(doc->CreatePathGeometry(u"M 30,20 l 258.24,0 0,56.64 -258.24,0 Z"));
6//Creating a matrix is optional, it can be used for proper positioning
7path->set_RenderTransform(doc->CreateMatrix(0.7f, 0.f, 0.f, 0.7f, 0.f, 20.f));
8//Create Image Brush
9path->set_Fill(doc->CreateImageBrush(dataDir() + u"QL_logo_color.tif", System::Drawing::RectangleF(0.f, 0.f, 258.24f, 56.64f), System::Drawing::RectangleF(50.f, 20.f, 193.68f, 42.48f)));
10// Save resultant XPS document
11doc->Save(outDir() + u"AddImage_out.xps");
Add Tiled Image
The solution offers XpsPath Class, with which you can add image on XPS document. After creating a Matrix and an ImageBrush as above, then specify XpsTileMode.Tile mode. You can set Opacity value from 0 to 1 for the ImageBrush. Then save document. Following code snippet shows complete functionality to add tiled image on 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
3System::SharedPtr<XpsDocument> doc = System::MakeObject<XpsDocument>();
4// Tile image
5// ImageBrush filled rectangle in the right top bellow
6System::SharedPtr<XpsPath> path = doc->AddPath(doc->CreatePathGeometry(u"M 10,160 L 228,160 228,305 10,305"));
7path->set_Fill(doc->CreateImageBrush(dataDir() + u"R08LN_NN.jpg", System::Drawing::RectangleF(0.f, 0.f, 128.f, 96.f), System::Drawing::RectangleF(0.f, 0.f, 64.f, 48.f)));
8(System::DynamicCast<Aspose::Page::Xps::XpsModel::XpsImageBrush>(path->get_Fill()))->set_TileMode(Aspose::Page::Xps::XpsModel::XpsTileMode::Tile);
9path->get_Fill()->set_Opacity(0.5f);
10// Save resultant XPS document
11doc->Save(outDir() + u"AddTiledImage_out.xps");