.NET の EPUB ドキュメント読み取りライブラリ
オープンソース .NET APIで、EPUBファイルにアクセスし読み取り、内容を抽出し、C# アプリ内で EPUB を PDF に変換します。
EpubReader は、ソフトウェア開発者が自分の C# アプリケーション内で EPUB ドキュメントを開いて読み取ることを可能にする、非常に強力なオープンソースの .NET ライブラリです。このライブラリは 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ライブラリは、ソフトウェア開発者がEPUBブックを読み込み、C# .NETコードを使用してプレーンテキストを抽出できるようにします。スムーズにこのタスクを実行するには、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);
}
}
}
}