Create XPS Document with Aspose.Page for .NET

Introduction

Welcome to our step-by-step guide on creating XPS documents using Aspose.Page for .NET. In this tutorial, we will explore the process of generating XPS files, a widely used format for electronic documents. Whether you’re a seasoned developer or just starting with Aspose.Page, this guide is tailored to help you seamlessly create XPS documents with clear examples and detailed explanations.

Prerequisites

Before we dive into the tutorial, ensure you have the following prerequisites in place:

  1. Aspose.Page for .NET Library: Download and install the Aspose.Page library from the download link.

  2. Your Document Directory: Choose or create a directory on your system where you want to save the output XPS files.

Now, let’s jump into the tutorial!

Import Namespaces

In order to use Aspose.Page for .NET, you need to import the necessary namespaces into your project. Follow these steps:

Step 1: Add Reference to Aspose.Page

In your project, add a reference to the Aspose.Page for .NET library. You can find the required DLL in the downloaded package.

Step 2: Import Namespaces

Include the following namespaces in your code file:

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

Now that we have set up the prerequisites and imported the necessary namespaces, let’s break down the process of creating an XPS document into multiple steps.

Step 1: Set Document Directory

string dir = "Your Document Directory";

Ensure to replace "Your Document Directory" with the actual path where you want to save the output XPS file.

Step 2: Create XPS Document

XpsDocument xDocs = new XpsDocument();

Initialize a new XPS document using the XpsDocument class.

Step 3: Add Glyphs to the Document

var glyphs = xDocs.AddGlyphs("Arial", 12, FontStyle.Regular, 300f, 450f, "Hello World!");

Use the AddGlyphs method to add glyphs (text) to the document. Customize the font, size, style, and position as needed.

Step 4: Set Glyph Fill Color

glyphs.Fill = xDocs.CreateSolidColorBrush(Color.Black);

Specify the fill color for the added glyphs. In this example, we use black, but you can choose any color.

Step 5: Save the Result

xDocs.Save(dir + "output.xps");

Finally, save the XPS document to the specified directory with the desired filename. The resulting XPS file will contain the “Hello World!” text.

Congratulations! You’ve successfully created an XPS document using Aspose.Page for .NET.

Conclusion

In this tutorial, we walked through the process of creating XPS documents using Aspose.Page for .NET. This powerful library provides a seamless way to generate electronic documents with ease. Experiment with different fonts, styles, and content to tailor your XPS files to your specific needs.

FAQ’s

Q1: Can I use custom fonts in my XPS document?

A1: Yes, you can specify the font family and size when adding glyphs to your XPS document.

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

A2: Yes, Aspose.Page supports .NET Core, allowing you to use it in cross-platform applications.

Q3: How can I add images to an XPS document?

A3: Aspose.Page provides methods to add images to your XPS document. Refer to the documentation for detailed examples.

Q4: Can I create multi-page XPS documents?

A4: Absolutely! You can add multiple pages to your XPS document using the Aspose.Page library.

Q5: Is there a trial version available?

A5: Yes, you can explore the features of Aspose.Page by downloading the free trial.