Insert image into XPS file
C# .NET API solution to work with images of XPS files
When you are going to work with images in XPS files, there are a few things to keep in mind:
- XPS files support a lot of image formats, including JPEG, PNG, TIFF, GIF, and BMP. However, the format uses compressed versions of these formats, such as JPEG XR (also known as HD Photo) or JBIG2 (for black and white images). It is done to help reduce the file size of the result document.
XPS files store images at a fixed resolution (defined at the time of creation) so zooming may result in pixelation or blurriness.
Images in XPS files are usually embedded directly within the document itself, this ensures that the document remains self-contained and portable. However, if you want to modify or replace an image in the file, you typically need to edit the XPS document itself.
XPS files use a structure called the Open Packaging Conventions (OPC) to organize and store their content. Images within an XPS file are represented as individual parts within the OPC package, and they can have relationships with other resources such as text or annotations. Understanding these relationships is important when working with images in XPS files, as it affects how they are referenced and displayed within the document.
XPS files often use advanced image compression techniques to minimize file size while maintaining reasonable image quality, such as JPEG XR.
Keep in mind these aspects to effectively handle and manipulate images within XPS documents. But no matter what, - Aspose.Page provides you with the functionality to manage the fonts of XPS files easily. With this API you can add different images using C#. To learn more about how to deal with XPS files and how to work with images in XPS files in particular follow the documentation.
To add images to XPS documents we need:
Aspose.Page for .NET API which is a feature-rich, powerful, and easy-to-use document manipulation and conversion API for C# platform.
Open the NuGet package manager, and search for Aspose.Page and install. You may also use the following command from the Package Manager Console.
Package Manager Console Command
PM> Install-Package Aspose.Page
Steps to add images to a XPS file C#.
This code snippet demonstrates how to add an image to an XPS document using the XpsDocument class in C#. To see more examples go to Aspose.Page-for-.NET GitHub project .
- Set the path to the directory containing the documents.
- Create a new instance of the XpsDocument class, which represents an XPS document.
- Add an image using the XpsDocument.AddPath() Method.
- If needed, create a Matrix using XpsMatrix class. This matrix transformation is applied to the image path to properly position and scale the image.
- Call the CreateImageBrush() Method that passes the path to the image file, source rectangle, and destination rectangle. The source rectangle defines the portion of the image file to be used, and the destination rectangle specifies the position and size of the image within the XpsPath.
- Save the changes using the Save() Method.
C# Code to insert an image to a XPS file
using Aspose.Page.XPS;
using Aspose.Page.XPS.XpsModel;
// Set the path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithImages();
// Create a new XPS Document
XpsDocument doc = new XpsDocument();
// Add an Image
XpsPath path = doc.AddPath(doc.CreatePathGeometry("M 30,20 l 258.24,0 0,56.64 -258.24,0 Z"));
//Creating a matrix is optional, it can be used for proper positioning
path.RenderTransform = doc.CreateMatrix(0.7f, 0f, 0f, 0.7f, 0f, 20f);
//Create an Image Brush
path.Fill = doc.CreateImageBrush(dataDir + "QL_logo_color.tif", new RectangleF(0f, 0f, 258.24f, 56.64f), new RectangleF(50f, 20f, 193.68f, 42.48f));
// Save the resultant XPS document
doc.Save(dataDir + "AddImage_out.xps");
FAQ
1. Can I add images to XPS files?
XPS is a fixed-layout document format developed by Microsoft, similar to PDF, and it supports the inclusion of images alongside text and other graphical elements. You can embed images directly into XPS files to enhance visual content and create more engaging documents.
2. How do I add images to an XPS file?
To add images to an XPS file, you can use various methods depending on the software or tools you’re using. In Aspose.Page it is XpsDocument.AddPath() Method of the XpsDocument Class.
3. What image formats are compatible with XPS files?
XPS files support a range of image formats for inclusion, including but not limited to JPEG, PNG, TIFF, GIF, and BMP. You can insert images in any of these formats into your XPS files, and they will be rendered and displayed correctly within the document when viewed using compatible XPS readers or software.
XPS What is XPS File Format
XPS format is similar to PDF format. Both are page description language (PDL) formats. EPS is based on HTML and not on PostScript language. The .eps file is capable to contain a markup of the document's structure along with the information on how the document would look like. There are also added instructions on how to print and render the document. The feature of the format is that it fixes the document's description which means that it will look the same no matter who and from what operational system opens it.