Esempio n. 1
0
        private static async Task <EpubByteContentFile> ReadByteContentFile(EpubContentFileRef contentFileRef)
        {
            var result = new EpubByteContentFile
            {
                FileName        = contentFileRef.FileName,
                ContentType     = contentFileRef.ContentType,
                ContentMimeType = contentFileRef.ContentMimeType,
                Content         = await contentFileRef.ReadContentAsBytesAsync().ConfigureAwait(false)
            };

            return(result);
        }
Esempio n. 2
0
        private void Load_Click(object sender, RoutedEventArgs e)
        {
            var openFileDialog = new Microsoft.Win32.OpenFileDialog()
            {
                Filter = "Epub Files (*.epub)|*.epub"
            };
            var result = openFileDialog.ShowDialog();

            if (result == true)
            {
                // Opens a book and reads all of its content into memory
                epubBook = EpubReader.ReadBook(openFileDialog.FileName);

                // COMMON PROPERTIES

                // Book's title
                string title = epubBook.Title;

                // Book's authors (comma separated list)
                string author = epubBook.Author;

                // Book's authors (list of authors names)
                List <string> authors = epubBook.AuthorList;

                // Book's cover image (null if there is no cover)
                byte[] coverImageContent = epubBook.CoverImage;
                if (coverImageContent != null)
                {
                    using (MemoryStream coverImageStream = new MemoryStream(coverImageContent.ToArray()))
                    {
                        // Assign the Source property of your image
                        try
                        {
                            File.Delete(openFileDialog.FileName + ".jpg");
                            File.WriteAllBytes(openFileDialog.FileName + ".jpg", coverImageStream.ToArray());
                        }
                        catch
                        {
                            File.WriteAllBytes(openFileDialog.FileName + ".jpg", coverImageStream.ToArray());
                        }
                        BitmapImage imageSource = new BitmapImage(new Uri(@"C:/Users/shish/Downloads/1.jpg", UriKind.Absolute));
                        Image1.Source = imageSource;
                    }
                }
                Info.Text       = "Title: " + title + "\n" + "Author: " + author + "\n";
                Info.FontWeight = FontWeights.Bold;

                // CHAPTERS

                // Enumerating chapters
                foreach (EpubChapter chapter in epubBook.Chapters)
                {
                    // Title of chapter
                    string chapterTitle = chapter.Title;

                    // HTML content of current chapter
                    string chapterHtmlContent = chapter.HtmlContent;

                    // Nested chapters
                    List <EpubChapter> subChapters = chapter.SubChapters;

                    //PrintChapter(chapter);

                    //Chapters.Inlines.Add(new Run(chapterTitle + "\n"));

                    Chapters.Items.Add(chapterTitle);
                }
                // CONTENT

                // Book's content (HTML files, stlylesheets, images, fonts, etc.)
                EpubContent bookContent = epubBook.Content;


                // IMAGES

                // All images in the book (file name is the key)
                Dictionary <string, EpubByteContentFile> images = bookContent.Images;

                EpubByteContentFile firstImage = images.Values.First();

                // Content type (e.g. EpubContentType.IMAGE_JPEG, EpubContentType.IMAGE_PNG)
                EpubContentType contentType = firstImage.ContentType;

                // MIME type (e.g. "image/jpeg", "image/png")
                string mimeContentType = firstImage.ContentMimeType;

                // Creating Image class instance from the content
                using (MemoryStream imageStream = new MemoryStream(firstImage.Content))
                {
                    System.Drawing.Image image = System.Drawing.Image.FromStream(imageStream);
                }


                // HTML & CSS

                // All XHTML files in the book (file name is the key)
                Dictionary <string, EpubTextContentFile> htmlFiles = bookContent.Html;

                // All CSS files in the book (file name is the key)
                Dictionary <string, EpubTextContentFile> cssFiles = bookContent.Css;

                // Entire HTML content of the book
                foreach (EpubTextContentFile htmlFile in htmlFiles.Values)
                {
                    string htmlContent = htmlFile.Content;
                }

                // All CSS content in the book
                foreach (EpubTextContentFile cssFile in cssFiles.Values)
                {
                    string cssContent = cssFile.Content;
                }


                // OTHER CONTENT

                // All fonts in the book (file name is the key)
                Dictionary <string, EpubByteContentFile> fonts = bookContent.Fonts;

                // All files in the book (including HTML, CSS, images, fonts, and other types of files)
                Dictionary <string, EpubContentFile> allFiles = bookContent.AllFiles;


                // ACCESSING RAW SCHEMA INFORMATION

                // EPUB OPF data
                EpubPackage package = epubBook.Schema.Package;

                // Enumerating book's contributors
                foreach (EpubMetadataContributor contributor in package.Metadata.Contributors)
                {
                    string contributorName = contributor.Contributor;
                    string contributorRole = contributor.Role;
                }

                // EPUB NCX data
                EpubNavigation navigation = epubBook.Schema.Navigation;

                // Enumerating NCX metadata
                foreach (EpubNavigationHeadMeta meta in navigation.Head)
                {
                    string metadataItemName    = meta.Name;
                    string metadataItemContent = meta.Content;
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Reading all E-Book files to memory structure EpubContent
        /// </summary>
        /// <param name="epubArchive"></param>
        /// <param name="book"></param>
        /// <returns></returns>
        public static EpubContent ReadContentFilesToMemory(ZipArchive epubArchive, EpubBook book)
        {
            EpubContent result = new EpubContent
            {
                Html     = new Dictionary <string, EpubTextContentFile>(),
                Css      = new Dictionary <string, EpubTextContentFile>(),
                Images   = new Dictionary <string, EpubByteContentFile>(),
                Fonts    = new Dictionary <string, EpubByteContentFile>(),
                AllFiles = new Dictionary <string, EpubContentFile>()
            };

            //double progress = 20;
            //double increment = (double)80 / book.Schema.Package.Manifest.Count;
            foreach (EpubManifestItem manifestItem in book.Schema.Package.Manifest)
            {
                string contentFilePath = ZipPathUtils.Combine(book.Schema.ContentDirectoryPath, manifestItem.Href);

                ZipArchiveEntry contentFileEntry = epubArchive.GetEntry(contentFilePath);
                if (contentFileEntry == null)
                {
                    throw new Exception(String.Format("EPUB parsing error: file {0} not found in archive.", contentFilePath));
                }
                if (contentFileEntry.Length > Int32.MaxValue)
                {
                    throw new Exception(String.Format("EPUB parsing error: file {0} is bigger than 2 Gb.", contentFilePath));
                }
                string          fileName        = manifestItem.Href;
                string          contentMimeType = manifestItem.MediaType;
                EpubContentType contentType     = GetContentTypeByContentMimeType(contentMimeType);
                switch (contentType)
                {
                case EpubContentType.XHTML_1_1:
                case EpubContentType.CSS:
                case EpubContentType.OEB1_DOCUMENT:
                case EpubContentType.OEB1_CSS:
                case EpubContentType.XHTML_1_1XML:
                case EpubContentType.DTBOOK:
                case EpubContentType.DTBOOK_NCX:
                    EpubTextContentFile epubTextContentFile = new EpubTextContentFile
                    {
                        FileName        = fileName,
                        ContentMimeType = contentMimeType,
                        ContentType     = contentType
                    };
                    using (Stream contentStream = contentFileEntry.Open())
                    {
                        if (contentStream == null)
                        {
                            throw new Exception(String.Format("Incorrect EPUB file: content file \"{0}\" specified in manifest is not found", fileName));
                        }
                        using (StreamReader streamReader = new StreamReader(contentStream))
                            epubTextContentFile.Content = streamReader.ReadToEnd();
                    }
                    switch (contentType)
                    {
                    case EpubContentType.XHTML_1_1:
                        result.Html.Add(fileName, epubTextContentFile);
                        break;

                    case EpubContentType.CSS:
                        result.Css.Add(fileName, epubTextContentFile);
                        break;
                    }
                    //В данный момент в AllFiles контент не попадает, так как отсутствует конвертация из EpubTextContentFile в EpubContentFile,
                    //а именно, нет конвертации из string в byte[]
                    result.AllFiles.Add(fileName, epubTextContentFile);
                    break;

                default:
                    EpubByteContentFile epubByteContentFile = new EpubByteContentFile
                    {
                        FileName        = fileName,
                        ContentMimeType = contentMimeType,
                        ContentType     = contentType
                    };
                    using (Stream contentStream = contentFileEntry.Open())
                    {
                        if (contentStream == null)
                        {
                            throw new Exception(String.Format("Incorrect EPUB file: content file \"{0}\" specified in manifest is not found", fileName));
                        }
                        using (MemoryStream memoryStream = new MemoryStream((int)contentFileEntry.Length))
                        {
                            contentStream.CopyTo(memoryStream);
                            epubByteContentFile.Content = memoryStream.ToArray();
                        }
                    }
                    switch (contentType)
                    {
                    case EpubContentType.IMAGE_GIF:
                    case EpubContentType.IMAGE_JPEG:
                    case EpubContentType.IMAGE_PNG:
                    case EpubContentType.IMAGE_SVG:
                        result.Images.Add(fileName, epubByteContentFile);
                        break;

                    case EpubContentType.FONT_TRUETYPE:
                    case EpubContentType.FONT_OPENTYPE:
                        result.Fonts.Add(fileName, epubByteContentFile);
                        break;
                    }
                    result.AllFiles.Add(fileName, epubByteContentFile);
                    break;
                }
            }
            return(result);
        }