Working with Images in XPS file | .NET
Contents
[
Hide
Show
]Add Image inside XPS Documents
Aspose.Page for .NET offers XpsPath class, with which you can add image on XPS document. You need to create XpsMatrix and XpsImageBrush and save XpsDocument. Following code snippet shows complete functionality to add image on an XPS document:
1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
2// The path to the documents directory.
3string dataDir = RunExamples.GetDataDir_WorkingWithImages();
4// Create new XPS Document
5XpsDocument doc = new XpsDocument();
6// Add Image
7XpsPath path = doc.AddPath(doc.CreatePathGeometry("M 30,20 l 258.24,0 0,56.64 -258.24,0 Z"));
8//Creating a matrix is optional, it can be used for proper positioning
9path.RenderTransform = doc.CreateMatrix(0.7f, 0f, 0f, 0.7f, 0f, 20f);
10//Create Image Brush
11path.Fill = doc.CreateImageBrush(dataDir + "QL_logo_color.tif", new RectangleF(0f, 0f, 258.24f, 56.64f), new RectangleF(50f, 20f, 193.68f, 42.48f));
12// Save resultant XPS document
13doc.Save(dataDir + "AddImage_out.xps");
The result of running this code is next
Add Tiled Image
The solution offers XpsPath class, with which you can add images on XPS documents. For this, you first need to create XpsMatrix and an XpsImageBrush, then specify XpsTileMode.Tile mode and save XpsDocument.
The following code snippet shows complete functionality to add a tiled image on an XPS document:
1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
2// The path to the documents directory.
3string dataDir = RunExamples.GetDataDir_WorkingWithImages();
4// Create new XPS Document
5XpsDocument doc = new XpsDocument();
6// Tile image
7// ImageBrush filled rectangle in the right top bellow
8XpsPath path = doc.AddPath(doc.CreatePathGeometry("M 10,160 L 228,160 228,305 10,305"));
9path.Fill = doc.CreateImageBrush(dataDir + "R08LN_NN.jpg", new RectangleF(0f, 0f, 128f, 96f), new RectangleF(0f, 0f, 64f, 48f));
10((XpsImageBrush)path.Fill).TileMode = XpsTileMode.Tile;
11path.Fill.Opacity = 0.5f;
12// Save resultant XPS document
13doc.Save(dataDir + "AddTiledImage_out.xps");
The result of running this code is next
You can download examples and data files from GitHub.