Biblioteca .NET para la lectura de documentos EPUB
API .NET de código abierto para acceder y leer archivos EPUB, extraer su contenido y convertir EPUB a PDF dentro de aplicaciones C#.
EpubReader es una biblioteca .NET de código abierto muy potente que permite a los desarrolladores de software abrir y leer documentos EPUB dentro de sus propias aplicaciones C#. La biblioteca soporta completamente los estándares EPUB 2 (2.0, 2.0.1) así como EPUB 3 (3.0, 3.0.1, 3.1, 3.2). La biblioteca EpubReader solo admite la lectura de archivos EPUB y, por el momento, no ofrece soporte de escritura.
La biblioteca EpubReader admite el análisis de libros EPUB y la extracción de contenidos sin dependencias externas. Un documento EPUB puede ser una colección de archivos HTML que incluyen CSS, imágenes, fuentes y más. Por lo tanto, puede renderizarse de la misma manera que lo hace un navegador web con otros archivos HTML. La biblioteca permite a los usuarios extraer el texto sin formato de todo el libro, soporte de análisis de capítulos, extraer la portada del libro y recorrer todos los archivos EPUB en un directorio para recopilar algunas estadísticas.
Comenzando con EpubReader
La forma recomendada de instalar EpubReader es usando NuGet. Por favor, use el siguiente comando para una instalación fluida.
Instalar EpubReader mediante NuGet
NuGet\Install-Package VersOne.Epub -Version 3.2.0
También puede instalarlo manualmente; descargue los archivos de la última versión directamente del repositorio GitHub.
Cómo cargar y leer un libro EPUB mediante la biblioteca .NET
La biblioteca de código abierto EpubReader admite completamente la carga y lectura de libros EPUB dentro de aplicaciones .NET. La biblioteca incluye numerosos métodos útiles para manejar libros EPUB, como cargar el libro en memoria, imprimir el título del libro, imprimir el autor del libro, imprimir la tabla de contenidos, imprimir un capítulo particular del libro, imprimir todos los capítulos del libro, y así sucesivamente. El siguiente ejemplo de código muestra cómo cargar e imprimir un libro EPUB usando la biblioteca.
Leer un libro EPUB mediante la biblioteca .NET
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();
}
Extraer texto plano de un libro EPUB mediante C#
La biblioteca de código abierto EpubReader permite a los desarrolladores de software cargar un libro EPUB y extraer texto plano de él usando código C# .NET. Tenga en cuenta que necesita instalar el paquete NuGet HtmlAgilityPack para realizar esta tarea sin problemas. El siguiente ejemplo muestra cómo extraer el texto plano de todo el libro con solo un par de líneas de código .NET.
Extraer texto sin formato de un libro EPUB mediante 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();
}
}
}
Extraer la tabla de contenidos de un libro EPUB mediante la API C#
La biblioteca de código abierto EpubReader permite a los desarrolladores de software cargar un libro EPUB y extraer texto plano de él usando código C# .NET. Tenga en cuenta que necesita instalar HtmlAgilityPack NuGet para realizar esta tarea sin problemas. El siguiente ejemplo muestra cómo extraer el texto plano de todo el libro con solo un par de líneas de código .NET.
¿Cómo extraer la tabla de contenido de un libro EPUB mediante la API .NET?
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);
}
}
}
}