Add Text to XPS Document with Aspose.Page for .NET

Introduction

In the dynamic world of .NET development, Aspose.Page stands out as a powerful tool for working with XPS documents. Adding text to XPS documents is a common requirement, and Aspose.Page simplifies this process. In this tutorial, we’ll explore how to use Aspose.Page for .NET to seamlessly add text to XPS documents.

Prerequisites

Before diving into the tutorial, make sure you have the following prerequisites in place:

  • Aspose.Page for .NET: Ensure that you have the Aspose.Page library installed. You can download it from the Aspose.Page for .NET documentation.

  • Development Environment: Set up your .NET development environment. If you haven’t done this yet, follow the installation instructions provided in the documentation.

  • Document Directory: Create a directory where you’ll store your documents. Replace “Your Document Directory” in the provided code snippet with the actual path.

Now, let’s move on to the step-by-step guide.

Import Namespaces

Firstly, let’s import the necessary namespaces to kickstart our project:

using Aspose.Page.XPS;
using Aspose.Page.XPS.XpsModel;
using System.Drawing;

Step 1: Create a New XPS Document

To start working with Aspose.Page, create a new XPS document. This will be the canvas where we add our text.

// ExStart:3
string dataDir = "Your Document Directory";
XpsDocument doc = new XpsDocument();
// ExEnd:3

Step 2: Create a Brush for Text

Now, let’s create a brush to define the text color. In this example, we’re using a black color brush.

// ExStart:4
XpsSolidColorBrush textFill = doc.CreateSolidColorBrush(Color.Black);
// ExEnd:4

Step 3: Add Glyphs to the Document

Glyphs represent the text in XPS documents. Add glyphs to the document with the desired font, size, style, and position.

// ExStart:5
XpsGlyphs glyphs = doc.AddGlyphs("Arial", 12, FontStyle.Regular, 300f, 450f, "Hello World!");
glyphs.Fill = textFill;
// ExEnd:5

Step 4: Save the Resultant XPS Document

Finally, save the XPS document with the added text to your specified directory.

// ExStart:6
doc.Save(dataDir + "AddText_out.xps");
// ExEnd:6

By following these simple steps, you’ve successfully added text to an XPS document using Aspose.Page for .NET.

Conclusion

In conclusion, Aspose.Page for .NET provides a straightforward solution for adding text to XPS documents in your .NET projects. The simplicity of the library, combined with its robust features, makes it an invaluable tool for document manipulation.

Frequently Asked Questions

Q1: Can I customize the font and size of the added text?

A1: Yes, you have full control over the font and size. Adjust the parameters in the AddGlyphs method accordingly.

Q2: Is Aspose.Page compatible with .NET Core?

A2: Absolutely! Aspose.Page supports .NET Core, ensuring compatibility with the latest .NET technologies.

Q3: Are there any licensing requirements for using Aspose.Page?

A3: Yes, you need a valid license. Explore licensing options here.

Q4: How can I get support or seek help?

A4: Visit the Aspose.Page forum to connect with the community and get assistance.

Q5: Is there a free trial available?

A5: Certainly! You can get a free trial here.