Example #1
0
        public void Export(Stream outputStream)
        {
            if (null == outputStream)
            {
                throw new ArgumentNullException("outputStream");
            }

            string lang = String.IsNullOrWhiteSpace(Language) ? CultureInfo.CurrentCulture.Name : Language;
            string uid  = String.IsNullOrWhiteSpace(UID) ? Guid.NewGuid().ToString() : UID;

            using (ZipArchive archive = new ZipArchive(outputStream, ZipArchiveMode.Create))
            {
                // Add mimetype
                ZipArchiveEntry mimetype = archive.CreateEntry("mimetype", CompressionLevel.NoCompression);
                using (StreamWriter sw = new StreamWriter(mimetype.Open()))
                {
                    sw.Write(@"application/epub+zip");
                }

                // Add container.xml
                ZipArchiveEntry containerXml = archive.CreateEntry("META-INF/container.xml", CompressionLevel.Optimal);
                using (StreamWriter sw = new StreamWriter(containerXml.Open()))
                {
                    sw.Write(ContainerXmlTemplate);
                }

                // Add content.opf
                ZipArchiveEntry contentOpf = archive.CreateEntry("OEBPS/content.opf", CompressionLevel.Optimal);
                using (StreamWriter sw = new StreamWriter(contentOpf.Open()))
                {
                    StringBuilder itemSB  = new StringBuilder();
                    StringBuilder spineSB = new StringBuilder();

                    for (int i = 0; i < _sections.Count; i++)
                    {
                        string sectionId = String.Format("section{0}", i + 1);
                        itemSB.AppendLine(String.Format(ContentOpfItemTemplate
                                                        , sectionId
                                                        , sectionId + ".html"
                                                        , "application/xhtml+xml"));

                        spineSB.AppendLine(String.Format(ContentOpfSpineItemRefTemplate, sectionId));
                    }

                    for (int i = 0; i < _resources.Count; i++)
                    {
                        string resourceId = String.Format("resource{0}", i + 1);
                        itemSB.AppendLine(String.Format(ContentOpfItemTemplate
                                                        , resourceId
                                                        , _resources[i].Path
                                                        , _resources[i].MediaType));
                    }

                    string content = String.Format(ContentOpfTemplate
                                                   , Title
                                                   , Author
                                                   , uid
                                                   , lang
                                                   , itemSB.ToString()
                                                   , spineSB.ToString());

                    sw.Write(content);
                }

                // Add toc.ncx
                ZipArchiveEntry tocNcx = archive.CreateEntry("OEBPS/toc.ncx", CompressionLevel.Optimal);
                using (StreamWriter sw = new StreamWriter(tocNcx.Open()))
                {
                    StringBuilder navLabelSB = new StringBuilder();

                    for (int i = 0; i < _sections.Count; i++)
                    {
                        EpubSection section = _sections[i];

                        string sectionId = String.Format("section{0}", i + 1);
                        navLabelSB.AppendLine(String.Format(TocNcxNavPointTemplate
                                                            , sectionId
                                                            , (i + 1).ToString()
                                                            , section.Title));
                    }

                    string content = String.Format(TocNcxTemplate
                                                   , uid
                                                   , Title
                                                   , navLabelSB.ToString());

                    sw.Write(content);
                }

                // Add Sections
                for (int i = 0; i < _sections.Count; i++)
                {
                    string sectionId = String.Format("section{0}", i + 1);

                    ZipArchiveEntry sectionHtml = archive.CreateEntry(String.Format("OEBPS/{0}.html", sectionId), CompressionLevel.Optimal);
                    using (StreamWriter sw = new StreamWriter(sectionHtml.Open()))
                    {
                        string content = "";

                        if (_sections[i].HasCss)
                        {
                            content = String.Format(EpubSectionHtmlWithCSSTemplate
                                                    , _sections[i].Title
                                                    , _sections[i].CssPath
                                                    , _sections[i].BodyHtml);
                        }
                        else
                        {
                            content = String.Format(EpubSectionHtmlTemplate
                                                    , _sections[i].Title
                                                    , _sections[i].BodyHtml);
                        }

                        sw.Write(content);
                    }
                }

                // Add Resources
                for (int i = 0; i < _resources.Count; i++)
                {
                    ZipArchiveEntry resourceEntry = archive.CreateEntry(String.Format("OEBPS/{0}", _resources[i].Path), CompressionLevel.Optimal);
                    _resources[i].ResourceStream.CopyTo(resourceEntry.Open());
                }
            }
        }
Example #2
0
        public void AddSection(string title, string body, string cssPath = "")
        {
            EpubSection section = new EpubSection(title, body, cssPath);

            _sections.Add(section);
        }