public override void SaveFileAs(string fileName, FileType type) { parsedEpub = new Document(); foreach (DictionaryEntry chapter in epub.Content) { var chapterContent = (eBdb.EpubReader.ContentData)chapter.Value; parsedEpub.AddXhtmlData(chapterContent.FileName, chapterContent.Content); } foreach (DictionaryEntry extendedData in epub.ExtendedData) { var dataContent = (eBdb.EpubReader.ExtendedData)extendedData.Value; if (dataContent.IsText && dataContent.FileName.EndsWith(".css")) { parsedEpub.AddStylesheetData(dataContent.FileName, dataContent.Content); } else if (!dataContent.IsText && dataContent.FileName.EndsWith(".jpg") || dataContent.FileName.EndsWith(".png") || dataContent.FileName.EndsWith(".bmp") || dataContent.FileName.EndsWith(".jpeg")) { parsedEpub.AddImageData(dataContent.FileName, Encoding.UTF8.GetBytes(dataContent.Content)); } } foreach (eBdb.EpubReader.NavPoint tocEntry in epub.TOC) { FillTocRecursively(tocEntry, null); } parsedEpub.AddAuthor(epub.Creator.JoinUsing(", ")); parsedEpub.AddBookIdentifier(epub.ID.JoinUsing(", ")); parsedEpub.AddDescription(epub.Description.JoinUsing(", ")); parsedEpub.AddFormat(epub.Format.JoinUsing(", ")); parsedEpub.AddLanguage(epub.Language.JoinUsing(", ")); parsedEpub.AddRelation(epub.Relation.JoinUsing(", ")); parsedEpub.AddRights(epub.Rights.JoinUsing(", ")); parsedEpub.AddSubject(epub.Subject.JoinUsing(", ")); parsedEpub.AddTitle(epub.Title.JoinUsing(", ")); parsedEpub.AddType(epub.Type.JoinUsing(", ")); parsedEpub.Generate(fileName); }
private static void GenerateEpub(string path, IReadOnlyList <Chapter> chaptersHtml, MetaInformation meta) { var epub = new Document(); epub.AddBookIdentifier(Guid.NewGuid().ToString()); epub.AddLanguage("English"); //TODO Publish date epub.AddMetaItem("dc:date", meta.PublishDate.ToString("YYYY-MM-DD")); epub.AddStylesheetData("style.css", Resources.style); epub.AddTitle(meta.Title); epub.AddAuthor(meta.Author); var chapterNumber = 0; if (meta.CoverImageFile?.Exists == true) { var coverImageId = epub.AddImageData(meta.CoverImageFile.Name, File.ReadAllBytes(meta.CoverImageFile.FullName)); epub.AddMetaItem("cover", coverImageId); var coverPage = Resources.epub_cover_template.Replace("{cover}", meta.CoverImageFile.Name); epub.AddXhtmlData($"page{chapterNumber}.xhtml", coverPage); epub.AddNavPoint("Cover", $"page{chapterNumber}.xhtml", chapterNumber); chapterNumber++; } var chapterTemplate = Resources.epub_template.Replace("{title}", meta.Title); for (var i = 0; i < chaptersHtml.Count; i++, chapterNumber++) { var chapter = chaptersHtml[i]; var chapterFile = $"page{chapterNumber}.xhtml"; var chapterContent = chapterTemplate.Replace("{body}", chapter.Html); epub.AddXhtmlData(chapterFile, chapterContent); epub.AddNavPoint(chapter.GetNavPoint(i + 1), chapterFile, chapterNumber); } epub.Generate(path); }