.NET 库,用于读取 EPUB 文档
开源 .NET API,用于访问并读取 EPUB 文件,提取其内容并在 C# 应用程序中将 EPUB 转换为 PDF。
EpubReader 是一个功能强大的开源 .NET 库,使软件开发者能够在自己的 C# 应用程序中打开和读取 EPUB 文档。该库完整支持 EPUB 标准 EPUB 2(2.0、2.0.1)以及 EPUB 3(3.0、3.0.1、3.1、3.2)。EpubReader 库目前仅支持读取 EPUB 文件,暂不提供写入支持。
EpubReader 库支持解析 EPUB 书籍并提取内容,无需任何外部依赖。EPUB 文档可以是包括 CSS、图像、字体等在内的 HTML 文件集合。因此,它可以像网页浏览器渲染其他 HTML 文件一样进行渲染。该库允许用户提取整本书的纯文本、支持章节解析、提取书籍封面,并遍历目录中的所有 EPUB 文件以收集一些统计信息。
EpubReader 入门指南
推荐的安装 EpubReader 方法是使用 NuGet。请使用以下命令进行顺利安装。
通过 NuGet 安装 EpubReader
NuGet\Install-Package VersOne.Epub -Version 3.2.0
您也可以手动安装;直接从 GitHub 仓库下载最新发布文件。
如何通过 .NET 库加载并阅读 EPUB 书籍
开源的 EpubReader 库完全支持在 .NET 应用程序中加载和读取 EPUB 电子书。该库包含了众多实用方法来处理 EPUB 电子书,例如将书加载到内存、打印书名、打印作者、打印目录、打印特定章节、打印全部章节等。以下代码示例演示了如何使用该库加载并打印 EPUB 电子书。
通过 .NET 库阅读 EPUB 书籍
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();
}
通过 C# 从 EPUB 书籍中提取纯文本
开源的 EpubReader 库使软件开发者能够使用 C# .NET 代码加载 EPUB 电子书并提取其中的纯文本。请注意,您需要安装 HtmlAgilityPack NuGet 以顺利完成此任务。以下示例演示了如何仅用几行 .NET 代码提取整本书的纯文本。
通过 C# 从 EPUB 电子书提取纯文本
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();
}
}
}
通过 C# API 从 EPUB 书籍中提取目录
开源的 EpubReader 库使软件开发人员能够使用 C# .NET 代码加载 EPUB 书籍并提取其中的纯文本。请注意,您需要安装 HtmlAgilityPack Nuget 以顺利完成此任务。以下示例演示了如何仅用几行 .NET 代码提取整本书的纯文本。
如何通过 .NET API 从 EPUB 电子书提取目录?
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);
}
}
}
}