
XPS files contain fixed page layout information including the layout, appearance, and printing information for a document. You can insert some text or image into an XPS file programmatically in C#.
- Insert Text or Image in an XPS Document – C# API Installation
- Add Text in XPS File using C#
- Add Text in XPS File using Unicode String in C#
- Insert Image in XPS Document using C#
- Place Tiled Image in XPS File using C#
Insert Text or Image in an XPS Document – C# API Installation
You can insert text or image in an XPS file without needing to install any word processor or other user-interface application. Simply configure Aspose.Page for .NET by downloading the DLL files from the Downloads section or using the NuGet installation command below:
PM> Install-Package Aspose.Page
Add Text in XPS File using C#
You can add any text in an XPS file by following the steps below:
- Initialize an object of the XPSDocument class.
- Create a brush of any color and add the glyph.
- Save output XPS document.
The following code snippet explains how to add text in an XPS file programmatically in C#:
// Create new XPS Document | |
XpsDocument doc = new XpsDocument(); | |
// Create a brush | |
XpsSolidColorBrush textFill = doc.CreateSolidColorBrush(Color.Black); | |
// Add glyph to the document | |
XpsGlyphs glyphs = doc.AddGlyphs("Arial", 12, FontStyle.Regular, 300f, 450f, "Hello World!"); | |
glyphs.Fill = textFill; | |
// Save resultant XPS document | |
doc.Save("AddText_out.xps"); |
Add Text in XPS File using Unicode String in C#
You can also add a Unicode string to the XPS document by following the steps below:
- Create an instance of the XPSDocument class.
- Add the Unicode string.
- Save the output XPS document.
The following code is based on these steps, which shows how to add text in an XPS file using a Unicode string in C# language:
// Create new XPS Document | |
XpsDocument doc = new XpsDocument(); | |
// Add Text | |
XpsSolidColorBrush textFill = doc.CreateSolidColorBrush(Color.Black); | |
Aspose.Page.XPS.XpsModel.XpsGlyphs glyphs = doc.AddGlyphs("Arial", 20, FontStyle.Regular, 400f, 200f, "TEN. rof egaP.esopsA"); | |
glyphs.BidiLevel = 1; | |
glyphs.Fill = textFill; | |
// Save resultant XPS document | |
doc.Save("AddText_out.xps"); |
Insert Image in XPS Document using C#
You can insert an image in the XPS document with the following steps:
- Create a new XPS Document.
- Load input Image.
- Create a Matrix and ImageBrush.
- Finally, save the output XPS file.
The code example below shows how to insert an image in an XPS document with C#:
// Create new XPS Document | |
XpsDocument doc = new XpsDocument(); | |
// Add 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 Image Brush | |
path.Fill = doc.CreateImageBrush("QL_logo_color.tif", new RectangleF(0f, 0f, 258.24f, 56.64f), new RectangleF(50f, 20f, 193.68f, 42.48f)); | |
// Save resultant XPS document | |
doc.Save("AddImage_out.xps"); |
Insert Tiled Image in XPS File using C#
You can insert the tiled image in the XPS file using C# by following the steps below:
- Create a new XPS Document.
- Add an ImageBrush-filled rectangle and add a tiled image.
- Save output XPS document.
The code snippet below elaborates on how to add the tiled image in an XPS file using C#:
// Create new XPS Document | |
XpsDocument doc = new XpsDocument(); | |
// ImageBrush filled rectangle in the right top below | |
XpsPath path = doc.AddPath(doc.CreatePathGeometry("M 10,160 L 228,160 228,305 10,305")); | |
path.Fill = doc.CreateImageBrush("R08LN_NN.jpg", new RectangleF(0f, 0f, 128f, 96f), new RectangleF(0f, 0f, 64f, 48f)); | |
((XpsImageBrush)path.Fill).TileMode = XpsTileMode.Tile; | |
path.Fill.Opacity = 0.5f; | |
// Save resultant XPS document | |
doc.Save("AddTiledImage_out.xps"); |
Conclusion
In this article, you have explored how to insert text or images in an XPS document using C#. You can add text as a simple string or a Unicode string as per your requirements. Likewise, an image can be inserted as usual or as a tiled image. Please feel free to visit the documentation section to learn other features of the API. In case of any concerns, please write to us at the forum.