Convert EPUB to PDF in .NET with Aspose.HTML
In this tutorial, we will explore how to use Aspose.HTML for .NET to convert EPUB files to PDF. Aspose.HTML is a powerful .NET library that provides various functionalities for working with HTML and EPUB documents. We will cover the prerequisites, import necessary namespaces, and break down multiple examples, explaining each step in detail.
Prerequisites
Before you begin, make sure you have the following prerequisites in place:
Aspose.HTML for .NET: Ensure you have Aspose.HTML for .NET installed in your .NET project. You can download it from here.
Your Data Directory: You will need a data directory where your EPUB files are stored.
Now, let’s dive into the step-by-step process of converting EPUB to PDF using Aspose.HTML for .NET.
Convert EPUB to PDF
Step 1: Initialize Your Project
Make sure you have a .NET project set up, and Aspose.HTML for .NET is installed.
Step 2: Import Required Namespaces
In your C# code file, import the necessary namespaces:
using Aspose.Html.Saving;
using Aspose.Html.Converters;
Step 3: Open the EPUB File
string dataDir = "Your Data Directory";
using (var stream = System.IO.File.OpenRead(dataDir + "input.epub"))
{
// Continue to the next step...
}
- Replace
"Your Data Directory"
with the actual directory where your EPUB file is located. - This code opens the EPUB file for reading.
Step 4: Set PDF Options and Perform Conversion
var options = new PdfSaveOptions();
Converter.ConvertEPUB(stream, options, "output.pdf");
- Create an instance of
PdfSaveOptions
to specify PDF conversion settings. - Use the
Converter.ConvertEPUB
method to convert the EPUB to PDF with the given options. - Save the resulting PDF as “output.pdf.”
Specify PDF Save Options
Step 1: Initialize Your Project
Ensure you have a .NET project set up and Aspose.HTML for .NET installed.
Step 2: Import Required Namespaces
Import the necessary namespaces in your C# code:
using Aspose.Html.Saving;
using Aspose.Html.Converters;
using Aspose.Html.IO;
using Aspose.Html.Drawing;
Step 3: Open the EPUB File
string dataDir = "Your Data Directory";
using (var stream = System.IO.File.OpenRead(dataDir + "input.epub"))
{
// Continue to the next step...
}
- Replace
"Your Data Directory"
with the actual directory of your EPUB file. - Open the EPUB file for reading.
Step 4: Customize PDF Options
var options = new PdfSaveOptions()
{
PageSetup = { AnyPage = new Page() { Size = Size.FromPixels(3000, 1000) } },
BackgroundColor = System.Drawing.Color.AliceBlue
};
- Create an instance of
PdfSaveOptions
and customize it as per your requirements. - In this example, we set the page size to 3000 pixels width and 1000 pixels height, and the background color to Alice Blue.
Step 5: Perform Conversion
Converter.ConvertEPUB(stream, options, "output.pdf");
- Use the
Converter.ConvertEPUB
method to convert the EPUB to PDF with the custom options. - Save the resulting PDF as “output.pdf.”
Use Custom Stream Provider
Step 1: Initialize Your Project
Set up a .NET project and install Aspose.HTML for .NET.
Step 2: Import Required Namespaces
In your C# code, import the necessary namespaces:
using Aspose.Html.Saving;
using Aspose.Html.Converters;
using Aspose.Html.IO;
Step 3: Open the EPUB File
string dataDir = "Your Data Directory";
using (var stream = System.IO.File.OpenRead(dataDir + "input.epub"))
{
// Continue to the next step...
}
- Replace
"Your Data Directory"
with the actual directory of your EPUB file. - Open the EPUB file for reading.
Step 4: Use a Custom Stream Provider
using (var streamProvider = new MemoryStreamProvider())
{
Converter.ConvertEPUB(stream, new PdfSaveOptions(), streamProvider);
// Continue to the next step...
}
- Create a
MemoryStreamProvider
to manage the conversion result as a memory stream. - Use the
Converter.ConvertEPUB
method with the custom stream provider.
Step 5: Save the Result
var memory = streamProvider.Streams.First();
memory.Seek(0, System.IO.SeekOrigin.Begin);
using (System.IO.FileStream fs = System.IO.File.Create("output.pdf"))
{
memory.CopyTo(fs);
}
- Access the memory stream containing the converted data.
- Set the stream position to the beginning.
- Create an output PDF file and copy the content from the memory stream to it.
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();
}
}
Now, you have learned how to convert EPUB files to PDF using Aspose.HTML for .NET with various options and customization possibilities. Aspose.HTML simplifies the process, providing flexibility and control over your document conversion.
Conclusion
Aspose.HTML for .NET is a versatile tool for converting EPUB files to PDF, offering customization options to tailor the resulting PDF document to your needs. Whether you require simple conversions or advanced customization, Aspose.HTML has you covered.
If you haven’t already, you can download Aspose.HTML for .NET from the website and start using it in your .NET applications today.
FAQs
Is Aspose.HTML for .NET free to use?
Aspose.HTML for .NET is a commercial product, but a free trial is available here.
Can I customize the appearance of the converted PDF?
Yes, you can customize the PDF appearance by adjusting options like page size and background color, as shown in Example 2.
How can I get support for Aspose.HTML for .NET?
You can find support and resources on the Aspose.HTML forum.
Are there other formats I can convert with Aspose.HTML for .NET?
Yes, Aspose.HTML for .NET supports various document formats, including HTML, EPUB, and more.
Is Aspose.HTML for ..NET suitable for large-scale document conversion?
Aspose.HTML for .NET is designed to handle large-scale document conversions efficiently, making it suitable for a wide range of applications.