.NET EPUB 문서 읽기 라이브러리
오픈 소스 .NET API를 통해 EPUB 파일에 접근하고 읽으며, 내용을 추출하고 C# 앱 내에서 EPUB를 PDF로 변환합니다.
EpubReader는 소프트웨어 개발자가 자체 C# 애플리케이션 내에서 EPUB 문서를 열고 읽을 수 있게 해주는 매우 강력한 오픈 소스 .NET 라이브러리입니다. 이 라이브러리는 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);
}
}
}
}