### Install EpubSharp via NuGet Source: https://github.com/asido/epubsharp/blob/master/README.md Use the Package Manager Console to install the library. ```powershell Install-Package EpubSharp.dll ``` -------------------------------- ### Create a New EPUB File Source: https://context7.com/asido/epubsharp/llms.txt Initializes a new EPUB document, sets metadata, adds a cover image, and includes chapters with HTML content. Supports outputting to a file, a stream, or a byte array. ```csharp using EpubSharp; using EpubSharp.Format; // Create a new EPUB writer var writer = new EpubWriter(); // Set metadata writer.SetTitle("My New Book"); writer.AddAuthor("John Doe"); writer.AddAuthor("Jane Smith"); // Add cover image byte[] coverImageData = File.ReadAllBytes("cover.png"); writer.SetCover(coverImageData, ImageFormat.Png); // Add chapters with HTML content writer.AddChapter("Introduction", @" Introduction

Introduction

Welcome to my book!

"); writer.AddChapter("Chapter 1", @" Chapter 1

Chapter 1: Getting Started

This is the first chapter of the book.

"); writer.AddChapter("Chapter 2", @" Chapter 2

Chapter 2: Advanced Topics

This chapter covers advanced topics.

", "chapter2"); // Custom file ID // Write to file writer.Write("my-new-book.epub"); // Or write to stream using (var stream = new MemoryStream()) { writer.Write(stream); byte[] epubBytes = stream.ToArray(); // Use epubBytes as needed } // Or get as byte array directly byte[] epubData = writer.Write(); ``` -------------------------------- ### Read EPUB from File Path Source: https://context7.com/asido/epubsharp/llms.txt Load an EPUB file from disk to access metadata, cover images, table of contents, and plain text content. ```csharp using EpubSharp; // Read an EPUB file from disk EpubBook book = EpubReader.Read("path/to/book.epub"); // Access basic metadata string title = book.Title; // "The Great Gatsby" IEnumerable authors = book.Authors; // ["F. Scott Fitzgerald"] // Access cover image (if available) byte[] coverImage = book.CoverImage; if (coverImage != null) { File.WriteAllBytes("cover.png", coverImage); } // Get table of contents with chapter hierarchy IList chapters = book.TableOfContents; foreach (var chapter in chapters) { Console.WriteLine($"Chapter: {chapter.Title}"); Console.WriteLine($" Path: {chapter.AbsolutePath}"); Console.WriteLine($" Subchapters: {chapter.SubChapters.Count}"); } // Convert entire book to plain text string plainText = book.ToPlainText(); Console.WriteLine(plainText); ``` -------------------------------- ### Setting Cover Images with ImageFormat Source: https://context7.com/asido/epubsharp/llms.txt Shows how to use the ImageFormat enumeration when assigning a cover image to an EpubWriter instance. ```csharp using EpubSharp; var writer = new EpubWriter(); // Set cover with different image formats byte[] gifCover = File.ReadAllBytes("cover.gif"); writer.SetCover(gifCover, ImageFormat.Gif); byte[] pngCover = File.ReadAllBytes("cover.png"); writer.SetCover(pngCover, ImageFormat.Png); byte[] jpegCover = File.ReadAllBytes("cover.jpg"); writer.SetCover(jpegCover, ImageFormat.Jpeg); byte[] svgCover = File.ReadAllBytes("cover.svg"); writer.SetCover(svgCover, ImageFormat.Svg); ``` -------------------------------- ### Navigating EPUB Chapters in C# Source: https://context7.com/asido/epubsharp/llms.txt Use the EpubChapter class to traverse the table of contents hierarchy or iterate sequentially. ```csharp using EpubSharp; EpubBook book = EpubReader.Read("book.epub"); // Navigate chapter hierarchy foreach (var chapter in book.TableOfContents) { PrintChapter(chapter, 0); } void PrintChapter(EpubChapter chapter, int depth) { string indent = new string(' ', depth * 2); Console.WriteLine($"{indent}Chapter: {chapter.Title}"); Console.WriteLine($"{indent} ID: {chapter.Id}"); Console.WriteLine($"{indent} AbsolutePath: {chapter.AbsolutePath}"); Console.WriteLine($"{indent} RelativePath: {chapter.RelativePath}"); Console.WriteLine($"{indent} HashLocation: {chapter.HashLocation}"); // Navigate relationships if (chapter.Parent != null) Console.WriteLine($"{indent} Parent: {chapter.Parent.Title}"); if (chapter.Previous != null) Console.WriteLine($"{indent} Previous: {chapter.Previous.Title}"); if (chapter.Next != null) Console.WriteLine($"{indent} Next: {chapter.Next.Title}"); // Process sub-chapters recursively foreach (var subChapter in chapter.SubChapters) { PrintChapter(subChapter, depth + 1); } } // Navigate sequentially through all chapters var currentChapter = book.TableOfContents.FirstOrDefault(); while (currentChapter != null) { Console.WriteLine($"Reading: {currentChapter.Title}"); currentChapter = currentChapter.Next; } ``` -------------------------------- ### Write EPUB Files Source: https://github.com/asido/epubsharp/blob/master/README.md Create a new EPUB file with author and cover information. Note that editing capabilities are currently limited. ```csharp EpubWriter writer = new EpubWriter(); writer.AddAuthor("Foo Bar"); writer.SetCover(imgData, ImageFormat.Png); writer.Write("new.epub"); ``` -------------------------------- ### Write EPUB Directly Source: https://context7.com/asido/epubsharp/llms.txt Uses static convenience methods to write an existing EpubBook object directly to a file or stream without manual writer instantiation. ```csharp using EpubSharp; // Read and write directly without modifications EpubBook book = EpubReader.Read("source.epub"); EpubWriter.Write(book, "copy.epub"); // Write to stream using (var stream = new FileStream("output.epub", FileMode.Create)) { EpubWriter.Write(book, stream); } ``` -------------------------------- ### Accessing EpubContentType and Filtering Resources Source: https://context7.com/asido/epubsharp/llms.txt Demonstrates the available EpubContentType values and how to filter book resources based on a specific content type. ```csharp using EpubSharp.Format; // Available content types EpubContentType xhtml = EpubContentType.Xhtml11; // application/xhtml+xml EpubContentType css = EpubContentType.Css; // text/css EpubContentType gif = EpubContentType.ImageGif; // image/gif EpubContentType jpeg = EpubContentType.ImageJpeg; // image/jpeg EpubContentType png = EpubContentType.ImagePng; // image/png EpubContentType svg = EpubContentType.ImageSvg; // image/svg+xml EpubContentType ttf = EpubContentType.FontTruetype; // font/truetype EpubContentType otf = EpubContentType.FontOpentype; // font/opentype EpubContentType xml = EpubContentType.Xml; // application/xml EpubContentType other = EpubContentType.Other; // Other types // Example: Filter resources by content type EpubBook book = EpubReader.Read("book.epub"); var pngImages = book.Resources.Images .Where(img => img.ContentType == EpubContentType.ImagePng) .ToList(); Console.WriteLine($"Found {pngImages.Count} PNG images"); ``` -------------------------------- ### Read and Process EPUB Files Source: https://github.com/asido/epubsharp/blob/master/README.md Access metadata, chapters, resources, and internal document structures from an EPUB file. ```csharp // Read an epub file EpubBook book = EpubReader.Read("my.epub"); // Read metadata string title = book.Title; string[] authors = book.Authors; Image cover = book.CoverImage; // Get table of contents ICollection chapters = book.TableOfContents; // Get contained files ICollection html = book.Resources.Html; ICollection css = book.Resources.Css; ICollection images = book.Resources.Images; ICollection fonts = book.Resources.Fonts; // Convert to plain text string text = book.ToPlainText(); // Access internal EPUB format specific data structures. EpubFormat format = book.Format; OcfDocument ocf = format.Ocf; OpfDocument opf = format.Opf; NcxDocument ncx = format.Ncx; NavDocument nav = format.Nav; // Create an EPUB EpubWriter.Write(book, "new.epub"); ``` -------------------------------- ### Accessing EPUB Format Structures in C# Source: https://context7.com/asido/epubsharp/llms.txt Access internal format documents such as OCF, OPF, NCX, and Nav for advanced manipulation. ```csharp using EpubSharp; using EpubSharp.Format; EpubBook book = EpubReader.Read("book.epub"); EpubFormat format = book.Format; // Access OCF (Container) document OcfDocument ocf = format.Ocf; Console.WriteLine($"Root file path: {ocf.RootFilePath}"); foreach (var rootFile in ocf.RootFiles) { Console.WriteLine($" {rootFile.FullPath} ({rootFile.MediaType})"); } // Access OPF (Package) document - metadata, manifest, spine OpfDocument opf = format.Opf; Console.WriteLine($"EPUB Version: {opf.EpubVersion}"); Console.WriteLine($"Unique ID: {opf.UniqueIdentifier}"); // Read detailed metadata Console.WriteLine($"Titles: {string.Join(", ", opf.Metadata.Titles)}"); Console.WriteLine($"Languages: {string.Join(", ", opf.Metadata.Languages)}"); Console.WriteLine($"Publishers: {string.Join(", ", opf.Metadata.Publishers)}"); Console.WriteLine($"Dates: {string.Join(", ", opf.Metadata.Dates.Select(d => d.Text))}"); Console.WriteLine($"Rights: {string.Join(", ", opf.Metadata.Rights)}"); // Access manifest items foreach (var item in opf.Manifest.Items) { Console.WriteLine($"Manifest: {item.Id} -> {item.Href} ({item.MediaType})"); } // Access spine (reading order) foreach (var itemRef in opf.Spine.ItemRefs) { Console.WriteLine($"Spine: {itemRef.IdRef}, Linear: {itemRef.Linear}"); } // Access NCX (Navigation Control) document for EPUB 2.x NcxDocument ncx = format.Ncx; if (ncx != null) { Console.WriteLine($"NCX Title: {ncx.DocTitle}"); Console.WriteLine($"NCX Author: {ncx.DocAuthor}"); foreach (var navPoint in ncx.NavMap.NavPoints) { Console.WriteLine($"NavPoint: {navPoint.NavLabelText} -> {navPoint.ContentSrc}"); } } // Access Nav document for EPUB 3.x NavDocument nav = format.Nav; if (nav != null) { Console.WriteLine($"Nav Title: {nav.Head.Title}"); } // Access file paths Console.WriteLine($"OCF Path: {format.Paths.OcfAbsolutePath}"); Console.WriteLine($"OPF Path: {format.Paths.OpfAbsolutePath}"); Console.WriteLine($"NCX Path: {format.Paths.NcxAbsolutePath}"); Console.WriteLine($"Nav Path: {format.Paths.NavAbsolutePath}"); ``` -------------------------------- ### Read EPUB from Stream Source: https://context7.com/asido/epubsharp/llms.txt Load an EPUB from a stream, with an option to keep the stream open after reading. ```csharp using EpubSharp; using System.IO; // Read from a FileStream using (var stream = File.OpenRead("book.epub")) { EpubBook book = EpubReader.Read(stream, leaveOpen: false); Console.WriteLine($"Title: {book.Title}"); } // Read from a MemoryStream (e.g., from network response) using (var memoryStream = new MemoryStream(downloadedBytes)) { EpubBook book = EpubReader.Read(memoryStream, leaveOpen: true); // Stream remains open for further processing } ``` -------------------------------- ### Add Custom Files to EPUB Source: https://context7.com/asido/epubsharp/llms.txt Integrates external assets such as CSS stylesheets, images, and fonts into the EPUB archive. Ensure correct EpubContentType is specified for each file type. ```csharp using EpubSharp; using EpubSharp.Format; var writer = new EpubWriter(); writer.SetTitle("Styled Book"); // Add CSS stylesheet (string content) writer.AddFile("styles/main.css", @" body { font-family: Georgia, serif; margin: 2em; line-height: 1.6; } h1 { color: #333; border-bottom: 1px solid #ccc; } p { text-indent: 1.5em; } ", EpubContentType.Css); // Add image (byte content) byte[] logoBytes = File.ReadAllBytes("logo.png"); writer.AddFile("images/logo.png", logoBytes, EpubContentType.ImagePng); byte[] jpegImage = File.ReadAllBytes("photo.jpg"); writer.AddFile("images/logo.jpeg", jpegImage, EpubContentType.ImageJpeg); // Add custom font byte[] fontBytes = File.ReadAllBytes("custom-font.ttf"); writer.AddFile("fonts/custom.ttf", fontBytes, EpubContentType.FontTruetype); byte[] otfFontBytes = File.ReadAllBytes("custom-font.otf"); writer.AddFile("fonts/custom.otf", otfFontBytes, EpubContentType.FontOpentype); // Add chapter referencing the stylesheet and images writer.AddChapter("Chapter 1", @" Chapter 1

Chapter 1

Logo

This chapter uses custom styling and images.

"); writer.Write("styled-book.epub"); ``` -------------------------------- ### Create Deep Copy of EPUB Source: https://context7.com/asido/epubsharp/llms.txt Generates a deep copy of an EpubBook by serializing and deserializing it through memory, allowing independent modifications. ```csharp using EpubSharp; EpubBook original = EpubReader.Read("book.epub"); // Create a deep copy EpubBook copy = EpubWriter.MakeCopy(original); // Modify copy without affecting original var writer = new EpubWriter(copy); writer.SetTitle("Copy of " + original.Title); writer.Write("book-copy.epub"); Console.WriteLine($"Original title: {original.Title}"); Console.WriteLine($"Copy title: {copy.Title}"); ``` -------------------------------- ### Access EPUB Resources Source: https://context7.com/asido/epubsharp/llms.txt Retrieve and process internal EPUB files such as HTML, CSS, images, and fonts. ```csharp using EpubSharp; EpubBook book = EpubReader.Read("book.epub"); // Access HTML content files IList htmlFiles = book.Resources.Html; foreach (var html in htmlFiles) { Console.WriteLine($"HTML File: {html.Href}"); Console.WriteLine($"MIME Type: {html.MimeType}"); Console.WriteLine($"Content Preview: {html.TextContent.Substring(0, 200)}..."); } // Access CSS stylesheets IList cssFiles = book.Resources.Css; foreach (var css in cssFiles) { Console.WriteLine($"CSS: {css.Href}"); Console.WriteLine($"Styles: {css.TextContent}"); } // Access images (as byte arrays) IList images = book.Resources.Images; foreach (var image in images) { Console.WriteLine($"Image: {image.Href}, Type: {image.ContentType}"); File.WriteAllBytes($"output/{image.Href}", image.Content); } // Access fonts IList fonts = book.Resources.Fonts; foreach (var font in fonts) { Console.WriteLine($"Font: {font.Href}, Size: {font.Content.Length} bytes"); } // Access all resources at once IList allResources = book.Resources.All; Console.WriteLine($"Total files in EPUB: {allResources.Count}"); ``` -------------------------------- ### Accessing EPUB Special Resources in C# Source: https://context7.com/asido/epubsharp/llms.txt Use the SpecialResources property to retrieve OCF, OPF, and HTML content ordered by the spine. ```csharp using EpubSharp; EpubBook book = EpubReader.Read("book.epub"); // Access OCF container file EpubTextFile ocf = book.SpecialResources.Ocf; Console.WriteLine($"OCF Path: {ocf.AbsolutePath}"); // Access OPF package document EpubTextFile opf = book.SpecialResources.Opf; Console.WriteLine($"OPF Content: {opf.TextContent}"); // Get HTML files in reading order (as defined in spine) IList orderedHtml = book.SpecialResources.HtmlInReadingOrder; foreach (var html in orderedHtml) { Console.WriteLine($"Reading order: {html.Href}"); } ``` -------------------------------- ### Read EPUB from Byte Array Source: https://context7.com/asido/epubsharp/llms.txt Load an EPUB file directly from memory using a byte array. ```csharp using EpubSharp; // Read EPUB from byte array byte[] epubBytes = File.ReadAllBytes("book.epub"); EpubBook book = EpubReader.Read(epubBytes); // Access book properties Console.WriteLine($"Title: {book.Title}"); Console.WriteLine($"Authors: {string.Join(", ", book.Authors)}"); ``` -------------------------------- ### EpubReader.Read Methods Source: https://context7.com/asido/epubsharp/llms.txt Methods for loading an EPUB file into an EpubBook object from various sources. ```APIDOC ## EpubReader.Read(string filePath) ### Description Reads an EPUB file from a specified file path. ### Parameters - **filePath** (string) - Required - The path to the EPUB file on disk. ### Response - **EpubBook** (object) - Returns an EpubBook object containing metadata, resources, and content. ## EpubReader.Read(byte[] epubData) ### Description Reads an EPUB file from a byte array. ### Parameters - **epubData** (byte[]) - Required - The raw byte content of the EPUB file. ### Response - **EpubBook** (object) - Returns an EpubBook object. ## EpubReader.Read(Stream stream, bool leaveOpen) ### Description Reads an EPUB file from a stream. ### Parameters - **stream** (Stream) - Required - The input stream containing the EPUB data. - **leaveOpen** (bool) - Required - Specifies whether the stream should remain open after reading. ### Response - **EpubBook** (object) - Returns an EpubBook object. ``` -------------------------------- ### Modify Existing EPUB Files Source: https://context7.com/asido/epubsharp/llms.txt Loads an existing EPUB book into the writer to update metadata, replace covers, or modify chapter content. ```csharp using EpubSharp; using EpubSharp.Format; // Read existing EPUB EpubBook book = EpubReader.Read("existing-book.epub"); // Create writer from existing book var writer = new EpubWriter(book); // Modify metadata writer.SetTitle("Updated Book Title"); writer.ClearAuthors(); writer.AddAuthor("New Author"); // Replace cover image byte[] newCover = File.ReadAllBytes("new-cover.png"); writer.SetCover(newCover, ImageFormat.Png); // Clear all chapters and add new ones writer.ClearChapters(); writer.AddChapter("New Chapter 1", "

New Content

"); // Save as new file writer.Write("modified-book.epub"); // Or overwrite original writer.Write("existing-book.epub"); ``` -------------------------------- ### EpubBook.Resources Access Source: https://context7.com/asido/epubsharp/llms.txt Accessing the various resources contained within an EPUB archive. ```APIDOC ## EpubBook.Resources ### Description Provides access to all files contained within the EPUB archive, categorized by type. ### Properties - **Html** (IList) - List of HTML content files. - **Css** (IList) - List of CSS stylesheets. - **Images** (IList) - List of image files. - **Fonts** (IList) - List of font files. - **All** (IList) - List of all files contained in the EPUB. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.