1. Products
  2.   eBook
  3.   .NET
  4.   EpubReader
 
  

.NET Library for EPUB Documents Reading

Open Source .NET API to Access & Read EPUB file, extract its contents and convert EPUB to PDF inside C# apps.

EpubReader is a very powerful open source .NET library that enables software developers to open and read EPUB documents inside their own C# applications. The library fully supports EPUB standards EPUB 2 (2.0, 2.0.1) as well as EPUB 3 (3.0, 3.0.1, 3.1, 3.2). The EpubReader library only supports reading EPUB files and it doesn't provide writing support for the time being.

The EpubReader library supports parsing EPUB books and extracted contents without any external dependencies. An EPUB document can be a collection of HTML files including CSS, images, fonts & more. So it can be rendered the same way as a web browser does with other HTML files. The library allows users to extract the plain text of the whole book, chapter parsing support, extract book cover, and iterate over all EPUB files in a directory and gather some stats.

Previous Next

Getting Started with EpubReader

The recommended way to install EpubReader is using NuGet. Please use the following command a smooth installation.

Install EpubReader via NuGet

NuGet\Install-Package VersOne.Epub -Version 3.2.0 

You can also install it manually; download the latest release files directly from GitHub repository.

How to Load & Read EPUB Book via.NET Library

The open source EpubReader library fully supports loading and reading EPUB books inside .NET applications. The library has included numerous useful methods for handling EPUB books, such as loading book into memory, print title of the book, print author of the book, print table of contents, print a particular chapter of the book, print all chapters of the book, and so on. The following code example demonstrates how to load and print an EPUB book using the library.

Read EPUB Book via .NET Library

using System.Text;
using VersOne.Epub;
using HtmlAgilityPack;

// Load the book into memory
EpubBook book = EpubReader.ReadBook("test.epub");

// Print the title and the author of the book
Console.WriteLine($"Title: {book.Title}");
Console.WriteLine($"Author: {book.Author}");
Console.WriteLine();

// Print the table of contents
Console.WriteLine("TABLE OF CONTENTS:");
PrintTableOfContents();
Console.WriteLine();

// Print the text content of all chapters in the book
Console.WriteLine("CHAPTERS:");
PrintChapters();

void PrintTableOfContents()
{
    foreach (EpubNavigationItem navigationItem in book.Navigation)
    {
        PrintNavigationItem(navigationItem, 0);
    }
}

void PrintNavigationItem(EpubNavigationItem navigationItem, int identLevel)
{
    Console.Write(new string(' ', identLevel * 2));
    Console.WriteLine(navigationItem.Title);
    foreach (EpubNavigationItem nestedNavigationItem in navigationItem.NestedItems)
    {
        PrintNavigationItem(nestedNavigationItem, identLevel + 1);
    }
}

void PrintChapters()
{
    foreach (EpubTextContentFile textContentFile in book.ReadingOrder)
    {
        PrintTextContentFile(textContentFile);
    }
}

void PrintTextContentFile(EpubTextContentFile textContentFile)
{
    HtmlDocument htmlDocument = new();
    htmlDocument.LoadHtml(textContentFile.Content);
    StringBuilder sb = new();
    foreach (HtmlNode node in htmlDocument.DocumentNode.SelectNodes("//text()"))
    {
        sb.AppendLine(node.InnerText.Trim());
    }
    string contentText = sb.ToString();
    Console.WriteLine(contentText);
    Console.WriteLine();
}

Extract Plain Text from a EPUB Book via C#

The open source EpubReader library enables software developers to load an EPUB Book and extract plain text from it using C# .NET code. Please note that you need to install HtmlAgilityPack Nuget in order to perform this task smoothly. The following example demonstrates how to extract the plain text of the whole book with just a couple of lines of .NET code.

Extract Plain Text from a EPUB Book via C#

using System;
using System.Text;
using HtmlAgilityPack;

namespace VersOne.Epub.ConsoleDemo
{
    internal static class ExtractPlainText
    {
        public static void Run(string filePath)
        {
            EpubBook book = EpubReader.ReadBook(filePath);
            foreach (EpubTextContentFile textContentFile in book.ReadingOrder)
            {
                PrintTextContentFile(textContentFile);
            }
        }

        private static void PrintTextContentFile(EpubTextContentFile textContentFile)
        {
            HtmlDocument htmlDocument = new();
            htmlDocument.LoadHtml(textContentFile.Content);
            StringBuilder sb = new();
            foreach (HtmlNode node in htmlDocument.DocumentNode.SelectNodes("//text()"))
            {
                sb.AppendLine(node.InnerText.Trim());
            }
            string contentText = sb.ToString();
            Console.WriteLine(contentText);
            Console.WriteLine();
        }
    }
}

Extract Table of Contents from EPUB Book via C# API

The open source EpubReader library enables software developers to load an EPUB Book and extract plain text from it using C# .NET code. Please note that you need to install HtmlAgilityPack Nuget in order to perform this task smoothly. The following example demonstrates how to extract the plain text of the whole book with just a couple of lines of .NET code.

How to Extract Table of Contents from EPUB Book via .NET API?

using System;

namespace VersOne.Epub.ConsoleDemo
{
    internal static class PrintNavigation
    {
        public static void Run(string filePath)
        {
            using (EpubBookRef bookRef = EpubReader.OpenBook(filePath))
            {
                Console.WriteLine("Navigation:");
                foreach (EpubNavigationItemRef navigationItemRef in bookRef.GetNavigation())
                {
                    PrintNavigationItem(navigationItemRef, 0);
                }
            }
            Console.WriteLine();
        }

        private static void PrintNavigationItem(EpubNavigationItemRef navigationItemRef, int identLevel)
        {
            Console.Write(new string(' ', identLevel * 2));
            Console.WriteLine(navigationItemRef.Title);
            foreach (EpubNavigationItemRef nestedNavigationItemRef in navigationItemRef.NestedItems)
            {
                PrintNavigationItem(nestedNavigationItemRef, identLevel + 1);
            }
        }
    }
}