Exemple #1
0
        public (HtmlToFlowDocument.Dom.FlowDocument Document, Dictionary <string, string> FontDictionary) OpenEbook(string fileName)
        {
            var epubBook = EpubReader.ReadBook(fileName);

            _bookContent = epubBook.Content;

            Dictionary <string, EpubTextContentFile> htmlFiles = _bookContent.Html;
            Dictionary <string, EpubTextContentFile> cssFiles  = _bookContent.Css;
            var readingOrder = epubBook.ReadingOrder;

            // ----------------- handle fonts ------------------------------
            var fontDictionary = new Dictionary <string, string>(); // Key is the font name, value is the absolute path to the font file
            var fontPath       = Path.Combine(_instanceStorageService.InstanceStoragePath, "Fonts");

            Directory.CreateDirectory(fontPath);

            foreach (var entry in _bookContent.Fonts)
            {
                var fontName     = entry.Key;
                var bytes        = entry.Value;
                var fontFileName = Path.GetFileName(entry.Value.FileName);
                fontFileName = Path.Combine(fontPath, fontFileName);
                using (var stream = new FileStream(fontFileName, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    var byteArray = bytes.Content;
                    stream.Write(byteArray, 0, byteArray.Length);
                }
                fontDictionary.Add(fontName, fontFileName);
            }

            // -------------------------------------------------------------

            string GetStyleSheet(string name, string htmlFileNameReferencedFrom)
            {
                EpubTextContentFile cssFile;
                // calculate absolute name with reference to htmlFileNameReferencedFrom
                var absoluteName = HtmlToFlowDocument.CssStylesheets.GetAbsoluteFileNameForFileRelativeToHtmlFile(name, htmlFileNameReferencedFrom);

                if (cssFiles.TryGetValue(absoluteName, out cssFile))
                {
                    return(cssFile.Content);
                }

                // if this could not resolve the name, then try to go to parent directories
                while (htmlFileNameReferencedFrom.Contains("/"))
                {
                    var idx = htmlFileNameReferencedFrom.LastIndexOf("/");
                    htmlFileNameReferencedFrom = htmlFileNameReferencedFrom.Substring(0, idx - 1);
                    absoluteName = HtmlToFlowDocument.CssStylesheets.GetAbsoluteFileNameForFileRelativeToHtmlFile(name, htmlFileNameReferencedFrom);
                    if (cssFiles.TryGetValue(absoluteName, out cssFile))
                    {
                        return(cssFile.Content);
                    }
                }

                // if this was not successful, then try it with the name alone
                if (cssFiles.TryGetValue(name, out cssFile))
                {
                    return(cssFile.Content);
                }

                return(null);
                // throw new ArgumentException($"CssFile {name} was not found!", nameof(name));
            }

            // Entire HTML content of the book
            var converter = new HtmlToFlowDocument.Converter()
            {
                AttachSourceAsTags = true
            };
            var flowDocument = new HtmlToFlowDocument.Dom.FlowDocument();

            foreach (EpubTextContentFile htmlFile in readingOrder)
            {
                string htmlContent = htmlFile.Content;
                var    textElement = converter.ConvertXHtml(htmlContent, false, GetStyleSheet, htmlFile.FileName); // create sections
                flowDocument.AppendChild(textElement);                                                             // and add them to the flow document
            }
            Settings.BookSettings.BookFileName = fileName;
            return(flowDocument, fontDictionary);
        }