|
2 | 2 | title: Convert EPUB to XPS in .NET with Aspose.HTML |
3 | 3 | linktitle: Convert EPUB to XPS in .NET with Aspose.HTML |
4 | 4 | second_title: Aspose.Slides .NET HTML manipulation API |
5 | | -description: |
| 5 | +description: Learn how to convert EPUB to XPS in .NET using Aspose.HTML for .NET. Follow our step-by-step guide for effortless conversions. |
6 | 6 | type: docs |
7 | 7 | weight: 13 |
8 | 8 | url: /net/html-extensions-and-conversions/convert-epub-to-xps-dotnet-aspose-html/ |
9 | 9 | --- |
10 | 10 |
|
11 | | -## Complete Source Code |
| 11 | +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! |
| 12 | + |
| 13 | +## Prerequisites |
| 14 | + |
| 15 | +Before you dive into the EPUB to XPS conversion process, you'll need to ensure that you have the following prerequisites in place: |
| 16 | + |
| 17 | +### 1. Aspose.HTML for .NET Library |
| 18 | + |
| 19 | +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](https://releases.aspose.com/html/net/). |
| 20 | + |
| 21 | +### 2. Input EPUB File |
| 22 | + |
| 23 | +You'll need an EPUB file that you want to convert to XPS. Ensure that you have an EPUB file available for conversion. |
| 24 | + |
| 25 | +### 3. .NET Development Environment |
| 26 | + |
| 27 | +This guide assumes you have a working .NET development environment set up on your machine. |
| 28 | + |
| 29 | +## Import Namespace |
| 30 | + |
| 31 | +To begin, you should import the necessary namespace for Aspose.HTML: |
| 32 | + |
12 | 33 | ```csharp |
13 | | - public static void WithASingleLine() |
14 | | - { |
15 | | - string dataDir = "Your Data Directory"; |
16 | | - // Open an existing EPUB file for reading. |
17 | | - using (var stream = System.IO.File.OpenRead(dataDir + "input.epub")) |
18 | | - { |
19 | | - // Call the ConvertEPUB method to convert the EPUB to XPS. |
20 | | - Aspose.Html.Converters.Converter.ConvertEPUB(stream, new Aspose.Html.Saving.XpsSaveOptions(), "output.xps"); |
21 | | - } |
22 | | - } |
23 | | - public static void ConvertEPUBFileToXPS() |
24 | | - { |
25 | | - string dataDir = "Your Data Directory"; |
26 | | - // Open an existing EPUB file for reading. |
27 | | - using (var stream = System.IO.File.OpenRead(dataDir + "input.epub")) |
28 | | - { |
29 | | - // Create an instance of XpsSaveOptions. |
30 | | - var options = new Aspose.Html.Saving.XpsSaveOptions(); |
31 | | - // Call the ConvertEPUB method to convert the EPUB to XPS. |
32 | | - Aspose.Html.Converters.Converter.ConvertEPUB(stream, options, "output.xps"); |
33 | | - } |
34 | | - } |
35 | | - public static void SpecifyXpsSaveOptions() |
36 | | - { |
37 | | - string dataDir = "Your Data Directory"; |
38 | | - // Open an existing EPUB file for reading. |
39 | | - using (var stream = System.IO.File.OpenRead(dataDir + "input.epub")) |
40 | | - { |
41 | | - // Create an instance of the XpsSaveOptions with a custom page-size and a background-color. |
42 | | - var options = new Aspose.Html.Saving.XpsSaveOptions() |
43 | | - { |
44 | | - PageSetup = |
45 | | - { |
46 | | - AnyPage = new Aspose.Html.Drawing.Page() |
47 | | - { |
48 | | - Size = new Aspose.Html.Drawing.Size(Aspose.Html.Drawing.Length.FromPixels(3000), Aspose.Html.Drawing.Length.FromPixels(1000)) |
49 | | - } |
50 | | - }, |
51 | | - BackgroundColor = System.Drawing.Color.AliceBlue, |
52 | | - }; |
53 | | - // Call the ConvertEPUB method to convert the EPUB to XPS. |
54 | | - Aspose.Html.Converters.Converter.ConvertEPUB(stream, options, "output.xps"); |
55 | | - } |
56 | | - } |
57 | | - public static void SpecifyCustomStreamProvider() |
| 34 | +using Aspose.Html.Saving; |
| 35 | +using Aspose.Html.Converters; |
| 36 | +using Aspose.Html.Drawing; |
| 37 | +``` |
| 38 | + |
| 39 | +## Convert EPUB to XPS |
| 40 | + |
| 41 | +Let's break down the process of converting an EPUB file to XPS format into multiple steps. |
| 42 | + |
| 43 | +### Step 1.1: Open the EPUB File |
| 44 | + |
| 45 | +First, open the existing EPUB file for reading using a FileStream: |
| 46 | + |
| 47 | +```csharp |
| 48 | +string dataDir = "Your Data Directory"; |
| 49 | +using (var stream = System.IO.File.OpenRead(dataDir + "input.epub")) |
| 50 | +{ |
| 51 | + // Continue with the conversion process |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +### Step 1.2: Create XpsSaveOptions |
| 56 | + |
| 57 | +Create an instance of XpsSaveOptions. This step is crucial for configuring the XPS output: |
| 58 | + |
| 59 | +```csharp |
| 60 | +var options = new XpsSaveOptions(); |
| 61 | +``` |
| 62 | + |
| 63 | +### Step 1.3: Convert EPUB to XPS |
| 64 | + |
| 65 | +Now, let's call the ConvertEPUB method to convert the EPUB to XPS: |
| 66 | + |
| 67 | +```csharp |
| 68 | +ConvertEPUB(stream, options, "output.xps"); |
| 69 | +``` |
| 70 | + |
| 71 | +## Specify Custom XPS Options |
| 72 | + |
| 73 | +You can further customize the XPS output by specifying custom options such as page size and background color. |
| 74 | + |
| 75 | +### Step 2.1: Custom Page Size and Background Color |
| 76 | + |
| 77 | +Create an instance of XpsSaveOptions with custom page size and background color: |
| 78 | + |
| 79 | +```csharp |
| 80 | +var options = new XpsSaveOptions() |
| 81 | +{ |
| 82 | + PageSetup = |
| 83 | + { |
| 84 | + AnyPage = new Page() |
58 | 85 | { |
59 | | - string dataDir = "Your Data Directory"; |
60 | | - // Open an existing EPUB file for reading. |
61 | | - using (var stream = System.IO.File.OpenRead(dataDir + "input.epub")) |
62 | | - { |
63 | | - // Create an instance of MemoryStreamProvider |
64 | | - using (var streamProvider = new MemoryStreamProvider()) |
65 | | - { |
66 | | - // Convert EPUB to XPS by using the MemoryStreamProvider |
67 | | - Aspose.Html.Converters.Converter.ConvertEPUB(stream, new Aspose.Html.Saving.XpsSaveOptions(), streamProvider); |
68 | | - // Get access to the memory stream that contains the resulted data |
69 | | - var memory = streamProvider.Streams.First(); |
70 | | - memory.Seek(0, System.IO.SeekOrigin.Begin); |
71 | | - // Flush the result data to the output file |
72 | | - using (System.IO.FileStream fs = System.IO.File.Create("output.xps")) |
73 | | - { |
74 | | - memory.CopyTo(fs); |
75 | | - } |
76 | | - } |
77 | | - } |
| 86 | + Size = new Size(Length.FromPixels(3000), Length.FromPixels(1000)) |
78 | 87 | } |
79 | | - class MemoryStreamProvider : Aspose.Html.IO.ICreateStreamProvider |
| 88 | + }, |
| 89 | + BackgroundColor = System.Drawing.Color.AliceBlue, |
| 90 | +}; |
| 91 | +``` |
| 92 | + |
| 93 | +### Step 2.2: Convert EPUB to XPS with Custom Options |
| 94 | + |
| 95 | +Now, call the ConvertEPUB method to convert the EPUB to XPS with the custom options: |
| 96 | + |
| 97 | +```csharp |
| 98 | +ConvertEPUB(stream, options, "output.xps"); |
| 99 | +``` |
| 100 | + |
| 101 | +## Use Custom Stream Provider |
| 102 | + |
| 103 | +In this step, we will convert EPUB to XPS using a custom stream provider, allowing you to manipulate the resulting data. |
| 104 | + |
| 105 | +### Step 3.1: Create a MemoryStreamProvider |
| 106 | + |
| 107 | +Create an instance of MemoryStreamProvider: |
| 108 | + |
| 109 | +```csharp |
| 110 | +using (var streamProvider = new MemoryStreamProvider()) |
| 111 | +{ |
| 112 | + // Continue with the conversion process |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +### Step 3.2: Convert EPUB to XPS with Stream Provider |
| 117 | + |
| 118 | +Convert EPUB to XPS by using the MemoryStreamProvider: |
| 119 | + |
| 120 | +```csharp |
| 121 | +ConvertEPUB(stream, new XpsSaveOptions(), streamProvider); |
| 122 | +``` |
| 123 | + |
| 124 | +### Step 3.3: Access and Save Result |
| 125 | + |
| 126 | +Retrieve the memory stream containing the converted data and save it to an output file: |
| 127 | + |
| 128 | +```csharp |
| 129 | +var memory = streamProvider.Streams.First(); |
| 130 | +memory.Seek(0, System.IO.SeekOrigin.Begin); |
| 131 | + |
| 132 | +using (System.IO.FileStream fs = System.IO.File.Create("output.xps")) |
| 133 | +{ |
| 134 | + memory.CopyTo(fs); |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +### Class MemoryStreamProvider Source Code |
| 139 | + |
| 140 | +```csharp |
| 141 | +class MemoryStreamProvider : Aspose.Html.IO.ICreateStreamProvider |
80 | 142 | { |
81 | 143 | // List of MemoryStream objects created during the document rendering |
82 | 144 | public List<System.IO.MemoryStream> Streams { get; } = new List<System.IO.MemoryStream>(); |
@@ -106,3 +168,27 @@ url: /net/html-extensions-and-conversions/convert-epub-to-xps-dotnet-aspose-html |
106 | 168 | } |
107 | 169 | } |
108 | 170 | ``` |
| 171 | +Congratulations! You've successfully converted an EPUB file to XPS format using Aspose.HTML for .NET. |
| 172 | + |
| 173 | +## Conclusion |
| 174 | + |
| 175 | +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. |
| 176 | + |
| 177 | +Have any questions or encountered issues? Check out the [Aspose.HTML Documentation](https://reference.aspose.com/html/net/) for more insights or seek help from the [Aspose.HTML Community Forum](https://forum.aspose.com/). |
| 178 | + |
| 179 | +## Frequently Asked Questions |
| 180 | + |
| 181 | +### What is Aspose.HTML for .NET? |
| 182 | +Aspose.HTML for .NET is a powerful library that enables developers to work with HTML, EPUB, and XPS documents in .NET applications. |
| 183 | + |
| 184 | +### Where can I download Aspose.HTML for .NET? |
| 185 | +You can download Aspose.HTML for .NET from the [official download page](https://releases.aspose.com/html/net/). |
| 186 | + |
| 187 | +### Is there a free trial available for Aspose.HTML for .NET? |
| 188 | +Yes, you can get a free trial from [here](https://releases.aspose.com/). |
| 189 | + |
| 190 | +### How can I obtain a temporary license for Aspose.HTML for .NET? |
| 191 | +To get a temporary license, visit the [temporary license page](https://purchase.aspose.com/temporary-license/). |
| 192 | + |
| 193 | +### Where can I find more tutorials and documentation for Aspose.HTML for .NET? |
| 194 | +Explore a wide range of tutorials and detailed documentation on the [Aspose.HTML Documentation](https://reference.aspose.com/html/net/) page. |
0 commit comments