Exemplo n.º 1
0
        public void Cosnowego()
        {
            var epub = new Document();

            // set metadata
            epub.AddAuthor("Jerome K. Jerome");
            epub.AddTitle("Three Men in a Boat (To Say Nothing of the Dog)");
            epub.AddLanguage("en");

            // embed fonts
            epub.AddFile("C:\\Fonts\\LiberationSerif-Regular.ttf",
              "fonts/LiberationSerif-Regular.ttf", "application/octet-stream");
            epub.AddFile("C:\\Fonts\\LiberationSerif-Bold.ttf",
              "fonts/LiberationSerif-Bold.ttf", "application/octet-stream");
            epub.AddFile("C:\\Fonts\\LiberationSerif-Italic.ttf",
              "LiberationSerif-Italic.ttf", "application/octet-stream");
            epub.AddFile("C:\\Fonts\\LiberationSerif-BoldItalic.ttf",
              "fonts/LiberationSerif-BoldItalic.ttf", "application/octet-stream");

            // Add stylesheet with @font-face
            epub.AddStylesheetFile("templates\\style.css", "style.css");

            // Add image files (figures)
            epub.AddImageFile("figures\\fig1.png", "fig1.png");
            epub.AddImageFile("figures\\drawing.svg", "drawing.svg");

            // add chapters' xhtml and setup TOC entries
            int navCounter = 1;
            for (int chapterCounter = 1; chapterCounter < 10; chapterCounter++)
            {
                String chapterFile = String.Format("page{0}.xhtml", chapterCounter);
                String chapterName = String.Format("Chapter {0}", chapterCounter);
                epub.AddXhtmlFile("tempdir\\" + chapterFile, chapterFile);
                var chapterTOCEntry =
                    epub.AddNavPoint(chapterName, chapterFile, navCounter++);
                // add nested TOC entries
                for (int part = 0; part < 3; part++)
                {
                    String partName = String.Format("Part {0}", part);
                    String partHref = chapterFile + String.Format("#{0}", part);
                    chapterTOCEntry.AddNavPoint(partName, partHref, navCounter++);
                }
            }

            // Generate resulting epub file
            epub.Generate("output\\mybook.epub");
        }
Exemplo n.º 2
0
        /*
         * wiz doc导出为html
         */
        private NavPoint ExportWizFolderDocuments(WizFolder wizfolder, string path, NavPoint nav, bool isroot, Document epub, int flags)
        {
            string strBy = cbBy.SelectedItem as string;
            string strSort = cbSort.SelectedItem as string;
            if (strBy == String.Empty || strSort == string.Empty)
                {
                    strBy = "Title";
                    strSort = "Asc";
                }

                WizDocumentCollection documents = wizfolder.GetDocuments3(0, strBy, strSort);
                //将文件导出为ziw,同时生成本节点的索引
                var categorycontent = new StringBuilder(25);
                string categoryhtml = File.ReadAllText(Path.Combine(Application.StartupPath, "page.xhtml"));

                categorycontent.Append("<h1>目录</h1>\r\n");
                categorycontent.Append("<h1>目录</h1>\r\n");

                string folderindexname = getFolderFileName(wizfolder.Name) + ".xhtml";

            NavPoint contentNav = null;

            if (nav == null)//根节点创建
            {
                if (isroot)
                {
                    folderindexname = "root.xhtml";
                    epub.AddNavPoint("目录", folderindexname, _playorder++);//创建节点
                    contentNav = null;
                }
                else
                {
                    contentNav = epub.AddNavPoint(wizfolder.Name, folderindexname, _playorder++);//创建节点
                }
            }
            else//子节点创建目录
            {
                contentNav = nav.AddNavPoint(wizfolder.Name, folderindexname, _playorder++);//创建节点
            }

            //第一次循环,生成catelog.html
            Hashtable hsTemp = new Hashtable();

            AddLog("正在生成目录:" + wizfolder.Name);
            foreach (WizDocument objDoc in documents)
            {
                string name = objDoc.Title;
                string fileindex = getDocumentFileName(name);
                string filename = fileindex + ".xhtml";

                hsTemp.Add(fileindex, objDoc);//加入到临时hashmap中

                StringBuilder stringBuilder = categorycontent.AppendFormat("<div><a href=\"{0}\">{1}</a></div>\r\n", filename, name);
            }

            categoryhtml = categoryhtml.Replace("%%TITLE%%", wizfolder.Name);
            categoryhtml = categoryhtml.Replace("%%CONTENT%%", categorycontent.ToString());

            epub.AddXhtmlData(folderindexname, categoryhtml);//写入content

            //需要对key先排序,fileindex定长,根据asc排序

            ArrayList keylist=  new ArrayList(hsTemp.Keys);
            keylist.Sort();

            //第二次循环再加入数据
            foreach (object key in keylist)
            {
                string fileindex = key as string;
                WizDocument objDoc = hsTemp[key] as WizDocument;

                string name = objDoc.Title;
                string filename =  fileindex + ".xhtml";
                string filefullname = Path.Combine(path, filename);

                AddLog("正在处理:" + wizfolder.Location + name);

                try
                {
                    objDoc.SaveToHtml(filefullname, flags);
                    html2xhtmlWithCss(filefullname);//转换为xhtml格式

                    epub.AddXhtmlFile(filefullname, filename);//写入到epub中

                    //加入到content中

                    //加入所有资源文件
                    string respath = fileindex + "_files";
                    string resfullpath = Path.Combine(path, respath);
                    if (Directory.Exists(resfullpath))//不存在则不处理子目录
                    {
                        //foreach(FileInfo imgfile in GetFiles(resfullpath, tbImageRule.Text))
                        //{
                        //    epub.AddImageFile(imgfile.FullName, Path.Combine(respath, imgfile.Name));
                        //}

                        //foreach(FileInfo cssFile in GetFiles(resfullpath, ".css;"))
                        //{
                        //    epub.AddStylesheetFile(cssFile.FullName, Path.Combine(respath, cssFile.Name));
                        //}
                        Parallel.ForEach(GetFiles(resfullpath, tbImageRule.Text), imgfile => epub.AddImageFile(imgfile.FullName, Path.Combine(respath, imgfile.Name)));

                        Parallel.ForEach(GetFiles(resfullpath, ".css;"), cssFile => epub.AddStylesheetFile(cssFile.FullName, Path.Combine(respath, cssFile.Name)));

                    }

                    //加入到navpoint中
                    if (isroot)//如果是根目录,则加入到根目录
                    {
                        epub.AddNavPoint(name, filename, _playorder++);
                    }
                    else
                    {
                        contentNav.AddNavPoint(name, filename, _playorder++);
                    }
                }
                catch (Exception e)
                {
                        AddLog("导出html失败:" + e.Message);
                }

                IncrementProgress();

            }

            return contentNav;//返回nav节点
            //categoryhtml

            //生成folder的索引文件
            //把子文档索引写进去
        }