Convert EPUB to XPS in C#

To convert EPUB to XPS in C#, open the EPUB file as a stream, create XpsSaveOptions, and call Converter.ConvertEPUB() with the stream, options, and output path or stream provider.

XPS is a fixed-layout document format used for consistent viewing, printing, and sharing. EPUB to XPS conversion is useful when a C# application needs a stable page representation from EPUB content while still controlling page size, margins, colors, and output streams.

Before running the examples, install and configure Aspose.HTML for .NET in your project. The complete C# example project is available in the Aspose.HTML for .NET GitHub repository.

Convert EPUB to XPS with Two Lines of C#

The static Converter class provides the shortest path from an EPUB file stream to an XPS file. The example below opens an EPUB file and converts it to XPS with default XpsSaveOptions.

1// Convert EPUB to XPS using C#
2
3// Open an existing EPUB file for reading
4using FileStream stream = File.OpenRead(DataDir + "input.epub");
5
6// Invoke the ConvertEPUB() method to convert EPUB to XPS
7Converter.ConvertEPUB(stream, new XpsSaveOptions(), Path.Combine(OutputDir, "convert-by-two-lines.xps"));

Use this pattern when the default XPS output is enough. For custom page setup, background color, or output streams, use the workflows below.

Convert an EPUB File to XPS in C#

A typical file-based conversion uses a readable EPUB file stream, an XpsSaveOptions object, and Converter.ConvertEPUB(). The conversion writes an XPS file to the specified output path.

The workflow is:

  1. Open the EPUB file with File.OpenRead() or another readable stream.
  2. Create an XpsSaveOptions instance.
  3. Call Converter.ConvertEPUB(stream, options, savePath) to write the XPS file.

The following C# code converts an EPUB file to XPS.

 1// Convert EPUB to XPS in C#
 2
 3// Open an existing EPUB file for reading
 4using FileStream stream = File.OpenRead(DataDir + "input.epub");
 5
 6// Prepare a path to save the converted file 
 7string savePath = Path.Combine(OutputDir, "input-output.xps");
 8
 9// Create an instance of XpsSaveOptions
10XpsSaveOptions options = new XpsSaveOptions();
11
12// Call the ConvertEPUB() method to convert EPUB to XPS
13Converter.ConvertEPUB(stream, options, savePath);

When to Use EPUB to XPS Conversion

Use XPS output when a C# application needs a fixed-layout document for Windows-oriented viewing, printing, or internal document pipelines. XPS is useful when page fidelity matters but the workflow expects XPS rather than PDF. If the output must be widely shared outside a Windows or XPS-aware environment, EPUB to PDF is often the more practical choice.

For XPS output, create XpsSaveOptions and pass it to Converter.ConvertEPUB() together with the EPUB stream and output path. You can tune page setup, CSS processing, background color, and rendering resolution through the options object.

Customize EPUB to XPS with XpsSaveOptions

Use XpsSaveOptions when the output XPS needs custom rendering settings. The options object is passed to ConvertEPUB() and controls how EPUB content is rendered to XPS.

OptionUse it to control
PageSetupPage size, margins, and page layout settings.
CssCSS processing behavior through CssOptions.
BackgroundColorThe color used to fill the page background. The default value is transparent.
HorizontalResolution and VerticalResolutionOutput resolution values used by the rendering process. The default value is 300 dpi for each property.

The following example creates an XPS file with custom XpsSaveOptions.

 1// Convert EPUB to XPS in C# with custom settings
 2
 3// Open an existing EPUB file for reading
 4using FileStream stream = File.OpenRead(DataDir + "input.epub");
 5
 6// Prepare a path to save the converted file 
 7string savePath = Path.Combine(OutputDir, "input-options.xps");
 8
 9// Create an instance of XpsSaveOptions. Set up the page-size and change the background color to LightGray 
10XpsSaveOptions options = new XpsSaveOptions()
11{
12    PageSetup =
13        {
14            AnyPage = new Page()
15            {
16                Size = new Aspose.Html.Drawing.Size(Length.FromPixels(500), Length.FromPixels(500))
17            }
18        },
19    BackgroundColor = System.Drawing.Color.LightGray
20};
21
22// Call the ConvertEPUB() method to convert EPUB to XPS
23Converter.ConvertEPUB(stream, options, savePath);

In this example, XpsSaveOptions is passed to Converter.ConvertEPUB() together with the EPUB stream and output path. The sample configures page setup and background color for the generated XPS document.

Related XPS Workflows

Save EPUB to XPS with an Output Stream Provider

Use ICreateStreamProvider when the conversion output should be written through streams instead of only to a file path. This is useful for memory-based workflows, remote storage, databases, and services that decide where the rendered output is stored.

The MemoryStreamProvider class below implements ICreateStreamProvider and keeps generated streams in memory.

 1// Implement a custom MemoryStream provider for advanced control over HTML rendering output streams
 2
 3class MemoryStreamProvider : Aspose.Html.IO.ICreateStreamProvider
 4{
 5    // List of MemoryStream objects created during the document rendering
 6    public List<MemoryStream> Streams { get; } = new List<MemoryStream>();
 7
 8    public Stream GetStream(string name, string extension)
 9    {
10        // This method is called when only one output stream is required, for instance for XPS, PDF or TIFF formats
11        MemoryStream result = new MemoryStream();
12        Streams.Add(result);
13        return result;
14    }
15
16    public Stream GetStream(string name, string extension, int page)
17    {
18        // This method is called when the creation of multiple output streams are required. For instance, during the rendering HTML to list of image files (JPG, PNG, etc.)
19        MemoryStream result = new MemoryStream();
20        Streams.Add(result);
21        return result;
22    }
23
24    public void ReleaseStream(Stream stream)
25    {
26        // Here you can release the stream filled with data and, for instance, flush it to the hard-drive
27    }
28
29    public void Dispose()
30    {
31        // Releasing resources
32        foreach (MemoryStream stream in Streams)
33            stream.Dispose();
34    }
35}

The following example converts EPUB to XPS with MemoryStreamProvider.

 1// Convert EPUB to XPS in C# using memory stream
 2
 3// Create an instance of MemoryStreamProvider
 4using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
 5
 6// Open an existing EPUB file for reading
 7using FileStream stream = File.OpenRead(DataDir + "input.epub");
 8
 9// Prepare a path to save the converted file 
10string savePath = Path.Combine(OutputDir, "stream-provider.xps");
11
12// Convert EPUB to XPS by using the MemoryStreamProvider class
13Converter.ConvertEPUB(stream, new XpsSaveOptions(), streamProvider);
14
15// Get access to the memory stream that contains the result data
16MemoryStream memory = streamProvider.Streams.First();
17memory.Seek(0, SeekOrigin.Begin);
18
19// Flush the result data to the output file
20using (FileStream fs = File.Create(savePath))
21{
22    memory.CopyTo(fs);
23}

The ConvertEPUB(Stream, XpsSaveOptions, ICreateStreamProvider) overload takes the EPUB source stream, XPS save options, and a stream provider used to create the output stream.

Related EPUB Conversion Guides

Other Platforms

Try Online EPUB to XPS Conversion

                
            

Use the online EPUB to XPS Converter for quick manual conversion. Use Aspose.HTML for .NET when EPUB to XPS conversion must run inside a C# application, service, or document pipeline.

EPUB to XPS Converter

FAQ

Can I convert EPUB to XPS for free?

You can evaluate Aspose.HTML for .NET before purchasing a license, but converted documents created in evaluation mode contain watermarks and are limited to four pages. For unrestricted testing, request a free 30-day Temporary License. For occasional manual conversions, use the free online EPUB to XPS Converter.

See Licensing Aspose.HTML for .NET for evaluation limitations and license setup.

Why does pagination change when EPUB is converted to XPS?

EPUB content is commonly reflowable, while XPS uses fixed pages. Page breaks therefore depend on the selected page size, margins, fonts, and rendered CSS. Configure XpsSaveOptions.PageSetup and make the EPUB fonts available in the conversion environment for more predictable pagination.