Convert EPUB to XPS in .NET with Aspose.HTML

Are you looking for a seamless way to convert EPUB files to XPS format in your .NET applications? Aspose.HTML for .NET provides a powerful solution to achieve this effortlessly. In this step-by-step guide, we will walk you through the process of converting EPUB to XPS using Aspose.HTML. Let’s get started!

Prerequisites

Before you dive into the EPUB to XPS conversion process, you’ll need to ensure that you have the following prerequisites in place:

1. Aspose.HTML for .NET Library

Make sure you have Aspose.HTML for .NET library installed in your project. If you haven’t done so, you can obtain it from the Aspose.HTML for .NET Download page.

2. Input EPUB File

You’ll need an EPUB file that you want to convert to XPS. Ensure that you have an EPUB file available for conversion.

3. .NET Development Environment

This guide assumes you have a working .NET development environment set up on your machine.

Import Namespace

To begin, you should import the necessary namespace for Aspose.HTML:

using Aspose.Html.Saving;
using Aspose.Html.Converters;
using Aspose.Html.Drawing;

Convert EPUB to XPS

Let’s break down the process of converting an EPUB file to XPS format into multiple steps.

Step 1.1: Open the EPUB File

First, open the existing EPUB file for reading using a FileStream:

string dataDir = "Your Data Directory";
using (var stream = System.IO.File.OpenRead(dataDir + "input.epub"))
{
    // Continue with the conversion process
}

Step 1.2: Create XpsSaveOptions

Create an instance of XpsSaveOptions. This step is crucial for configuring the XPS output:

var options = new XpsSaveOptions();

Step 1.3: Convert EPUB to XPS

Now, let’s call the ConvertEPUB method to convert the EPUB to XPS:

ConvertEPUB(stream, options, "output.xps");

Specify Custom XPS Options

You can further customize the XPS output by specifying custom options such as page size and background color.

Step 2.1: Custom Page Size and Background Color

Create an instance of XpsSaveOptions with custom page size and background color:

var options = new XpsSaveOptions()
{
    PageSetup =
    {
        AnyPage = new Page()
        {
            Size = new Size(Length.FromPixels(3000), Length.FromPixels(1000))
        }
    },
    BackgroundColor = System.Drawing.Color.AliceBlue,
};

Step 2.2: Convert EPUB to XPS with Custom Options

Now, call the ConvertEPUB method to convert the EPUB to XPS with the custom options:

ConvertEPUB(stream, options, "output.xps");

Use Custom Stream Provider

In this step, we will convert EPUB to XPS using a custom stream provider, allowing you to manipulate the resulting data.

Step 3.1: Create a MemoryStreamProvider

Create an instance of MemoryStreamProvider:

using (var streamProvider = new MemoryStreamProvider())
{
    // Continue with the conversion process
}

Step 3.2: Convert EPUB to XPS with Stream Provider

Convert EPUB to XPS by using the MemoryStreamProvider:

ConvertEPUB(stream, new XpsSaveOptions(), streamProvider);

Step 3.3: Access and Save Result

Retrieve the memory stream containing the converted data and save it to an output file:

var memory = streamProvider.Streams.First();
memory.Seek(0, System.IO.SeekOrigin.Begin);

using (System.IO.FileStream fs = System.IO.File.Create("output.xps"))
{
    memory.CopyTo(fs);
}

Class MemoryStreamProvider Source Code

class MemoryStreamProvider : Aspose.Html.IO.ICreateStreamProvider
        {
            // List of MemoryStream objects created during the document rendering
            public List<System.IO.MemoryStream> Streams { get; } = new List<System.IO.MemoryStream>();
            public System.IO.Stream GetStream(string name, string extension)
            {
                // This method is called when the only one output stream is required, for instance for XPS, PDF or TIFF formats.
                System.IO.MemoryStream result = new System.IO.MemoryStream();
                Streams.Add(result);
                return result;
            }
            public System.IO.Stream GetStream(string name, string extension, int page)
            {
                // This method is called when the creation of multiple output streams are required. For instance during the rendering HTML to list of the image files (JPG, PNG, etc.)
                System.IO.MemoryStream result = new System.IO.MemoryStream();
                Streams.Add(result);
                return result;
            }
            public void ReleaseStream(System.IO.Stream stream)
            {
                //  Here You can release the stream filled with data and, for instance, flush it to the hard-drive
            }
            public void Dispose()
            {
                // Releasing resources
                foreach (var stream in Streams)
                    stream.Dispose();
            }
        }

Congratulations! You’ve successfully converted an EPUB file to XPS format using Aspose.HTML for .NET.

Conclusion

In this comprehensive tutorial, we explored how to leverage Aspose.HTML for .NET to convert EPUB files to XPS format with various customization options. Whether you’re a seasoned developer or just starting, Aspose.HTML simplifies the process, allowing you to handle EPUB to XPS conversions with ease.

Have any questions or encountered issues? Check out the Aspose.HTML Documentation for more insights or seek help from the Aspose.HTML Community Forum.

Frequently Asked Questions

What is Aspose.HTML for .NET?

Aspose.HTML for .NET is a powerful library that enables developers to work with HTML, EPUB, and XPS documents in .NET applications.

Where can I download Aspose.HTML for .NET?

You can download Aspose.HTML for .NET from the download page.

Is there a free trial available for Aspose.HTML for .NET?

Yes, you can get a free trial from here.

How can I obtain a temporary license for Aspose.HTML for .NET?

To get a temporary license, visit the temporary license page.

Where can I find more tutorials and documentation for Aspose.HTML for .NET?

Explore a wide range of tutorials and detailed documentation on the Aspose.HTML Documentation page.