如何用.net c# 读取epub格式文件

如何用.net(c#)读取epub格式文件


epub格式是印刷出版界常见的格式,本格式遵循XML原则把网页进行压缩打包。

如何用c#读取epub格式文件是个头疼的问题,本人搜遍各大网站,发现介绍都语焉不详。

因项目中要用的此功能,特做案例整理如下,仅供参考。


// 动态库下载地址 https://epubreader.codeplex.com/,添加引用eBdb.EpubReader.dll

// 头部增加引用

       using eBdb.EpubReader;


        string fullfile = @"E:\佛学资料\EPUB\般若秒瓶.epub"  ;
        Epub epub = new Epub(@fullfile);

        //Get book title (Every epub file can have multiple titles)
        // 获取epub文章标题
        string title = epub.Title[0];

        //Get book authors (Every epub file can have multiple authors)
        // 获取作者
        string author = epub.Creator[0];

        //Get all book content as plain text
        // 以纯文本格式获取图书内容
        string plainText = epub.GetContentAsPlainText();

        //Get all book content as html text
        // 以html 格式获取图书内容
        string htmlText = epub.GetContentAsHtml();       

        //Get Table Of Contents (TOC)
        // 获取目录
        List<NavPoint> navPoints = epub.TOC;
        
        //获取目录数量
        int cnt = navPoints.Count;
        //获取目录序号
        int j=1;
        // 获取目录标题
        string contenttitle = navPoints[j].Title.ToString();
        
        // 获取子目录标题(假设有子目录)
        string childtitle = navPoints[j].Children[0].Title.ToString();

        //Get some part of book content
        // 获得部分文章章节,序号是把大小目录在一起排序的
        /*
         一、解义慧剑释   0
         * 1、第一课          1
         * 2、第二课          2  
         */
        ContentData contentData = epub.Content[j] as ContentData;
   
        //获取目录内容(默认只取第一级目录)

        // 获取文本格式内容
        string content_plaintext =
            navPoints[j].ContentData.GetContentAsPlainText().ToString();

        //获取HTML格式内容
        string content_html =
          navPoints[j].ContentData.Content.ToString();


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用C#中的EPUBReader库来读取EPUB文件,然后使用EPUBReader库提供的API来获取EPUB文件中的章节内容。接下来,你可以将每个章节的内容保存到单独的文件中,以便按章节分割EPUB文件。 以下是一个用于读取EPUB文件并按章节分割的示例代码: ```csharp using EPUBReader; using System; using System.Collections.Generic; using System.IO; namespace EPUBSplitter { class Program { static void Main(string[] args) { // Replace with the path to your EPUB file string epubPath = @"C:\example.epub"; // Create an EPUBReader object EPUBReader epubReader = new EPUBReader(epubPath); // Get the list of chapters in the EPUB file List<Chapter> chapters = epubReader.GetChapters(); // Create a directory to store the split files string outputDirectory = Path.GetDirectoryName(epubPath) + "\\output"; if (!Directory.Exists(outputDirectory)) { Directory.CreateDirectory(outputDirectory); } // Save each chapter to a separate file for (int i = 0; i < chapters.Count; i++) { Chapter chapter = chapters[i]; string chapterTitle = chapter.Title; string chapterContent = chapter.Content; // Remove any illegal characters from the chapter title foreach (char c in Path.GetInvalidFileNameChars()) { chapterTitle = chapterTitle.Replace(c.ToString(), ""); } // Create a file path for the chapter string chapterPath = outputDirectory + "\\" + chapterTitle + ".txt"; // Save the chapter content to the file File.WriteAllText(chapterPath, chapterContent); } Console.WriteLine("EPUB file split into " + chapters.Count + " chapters."); Console.ReadLine(); } } } ``` 这个代码将读取EPUB文件的内容,并将每个章节保存到一个单独的文件中。请注意,这个代码仅供参考,你需要根据自己的要求进行修改。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值