ไลบรารี .NET สำหรับการอ่านเอกสาร EPUB
API .NET โอเพ่นซอร์สสำหรับเข้าถึงและอ่านไฟล์ EPUB, ดึงเนื้อหาออกและแปลง EPUB เป็น PDF ภายในแอป C#
EpubReader เป็นไลบรารี .NET แบบโอเพนซอร์สที่มีประสิทธิภาพสูง ช่วยให้นักพัฒนาซอฟต์แวร์สามารถเปิดและอ่านเอกสาร EPUB ภายในแอปพลิเคชัน C# ของตนเองได้ ไลบรารีนี้รองรับมาตรฐาน EPUB อย่างเต็มที่ ทั้ง EPUB 2 (2.0, 2.0.1) และ EPUB 3 (3.0, 3.0.1, 3.1, 3.2) ไลบรารี EpubReader รองรับการอ่านไฟล์ EPUB เท่านั้นและในขณะนี้ยังไม่มีการสนับสนุนการเขียน
ไลบรารี EpubReader รองรับการพาร์สหนังสือ EPUB และเนื้อหาที่สกัดออกมาโดยไม่ต้องพึ่งพาไลบรารีภายนอก เอกสาร EPUB สามารถเป็นชุดของไฟล์ HTML รวมถึง CSS, รูปภาพ, ฟอนต์ และอื่น ๆ ดังนั้นจึงสามารถแสดงผลได้เช่นเดียวกับที่เว็บเบราว์เซอร์ทำกับไฟล์ HTML อื่น ๆ ไลบรารีนี้อนุญาตให้ผู้ใช้สกัดข้อความธรรมดาของหนังสือทั้งหมด, รองรับการพาร์สบท, สกัดปกหนังสือ, และวนลูปผ่านไฟล์ EPUB ทั้งหมดในไดเรกทอรีเพื่อรวบรวมสถิติบางอย่าง
เริ่มต้นใช้งาน EpubReader
วิธีที่แนะนำในการติดตั้ง EpubReader คือการใช้ NuGet. โปรดใช้คำสั่งต่อไปนี้เพื่อการติดตั้งที่ราบรื่น.
ติดตั้ง EpubReader ผ่าน NuGet
NuGet\Install-Package VersOne.Epub -Version 3.2.0
คุณยังสามารถติดตั้งด้วยตนเองได้; ดาวน์โหลดไฟล์เวอร์ชันล่าสุดโดยตรงจาก GitHub.
วิธีโหลดและอ่านหนังสือ EPUB ผ่านไลบรารี .NET
ไลบรารีโอเพ่นซอร์ส EpubReader รองรับการโหลดและอ่านหนังสือ EPUB อย่างเต็มรูปแบบภายในแอปพลิเคชัน .NET. ไลบรารีนี้ได้รวมเมธอดที่มีประโยชน์มากมายสำหรับการจัดการหนังสือ EPUB เช่น การโหลดหนังสือเข้าสู่หน่วยความจำ, พิมพ์ชื่อหนังสือ, พิมพ์ผู้เขียนของหนังสือ, พิมพ์สารบัญ, พิมพ์บทหนึ่งเฉพาะของหนังสือ, พิมพ์ทุกบทของหนังสือ, และอื่น ๆ. ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการโหลดและพิมพ์หนังสือ EPUB ด้วยไลบรารีนี้.
อ่านหนังสือ EPUB ผ่านไลบรารี .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();
}
สกัดข้อความธรรมดาจากหนังสือ EPUB ผ่าน C#
ไลบรารีโอเพ่นซอร์ส EpubReader ช่วยให้นักพัฒนาซอฟต์แวร์สามารถโหลดหนังสือ EPUB และดึงข้อความธรรมดาจากมันโดยใช้โค้ด C# .NET. โปรดทราบว่าคุณต้องติดตั้ง HtmlAgilityPack NuGet เพื่อทำงานนี้ได้อย่างราบรื่น. ตัวอย่างต่อไปนี้แสดงวิธีการดึงข้อความธรรมดาของหนังสือทั้งหมดด้วยเพียงไม่กี่บรรทัดของโค้ด .NET.
สกัดข้อความธรรมดาจากหนังสือ EPUB ผ่าน 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();
}
}
}
สกัดสารบัญจากหนังสือ EPUB ผ่าน API C#
ไลบรารีโอเพ่นซอร์ส EpubReader ช่วยให้นักพัฒนาซอฟต์แวร์สามารถโหลดหนังสือ EPUB และสกัดข้อความธรรมดาออกจากมันโดยใช้โค้ด C# .NET โปรดทราบว่าคุณต้องติดตั้ง HtmlAgilityPack Nuget เพื่อทำงานนี้ได้อย่างราบรื่น ตัวอย่างต่อไปนี้แสดงวิธีสกัดข้อความธรรมดาของหนังสือทั้งหมดด้วยเพียงไม่กี่บรรทัดของโค้ด .NET
วิธีสกัดสารบัญจากหนังสือ EPUB ผ่าน .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);
}
}
}
}