Working with Images | C++ API Solution

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 a Matrix and an ImageBrush to add the image to the XPS file. In order to add an image to XPS document, use the following steps.

  1. Create an object of XpsDocument class
  2. Create XpsPath object with defined Path Geometry
  3. Set Render Transformation for the path
  4. 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. You need to create Matrix and an ImageBrush, then specify  XpsTileMode.Tile mode and save XpsDocument. 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");
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.