public void SplitFile(ConversionOptions options)
 {
     _CurrentOptions = options;
     _WorkingDir = "";
     _HtmlFooter = _BodyEndTag;
     ProcessFile(_CurrentOptions.InputFile);
 }
        public static ConversionOptions FindMetaData(string html)
        {
            ConversionOptions returnable = new ConversionOptions();
            returnable.Title = ParseTag(html, "Title");
            returnable.Author = ParseMeta(html, "author");
            DateTime publish = DateTime.MinValue;
            DateTime.TryParse(ParseMeta(html, "date"), out publish);
            returnable.PublishDate = publish;

            return returnable;
        }
Example #3
0
        public static byte[] GetTOCFile(ConversionOptions options)
        {
            StringBuilder tocBuilder = new StringBuilder();

            tocBuilder.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

            //ncx
            tocBuilder.AppendLine("<ncx xmlns=\"http://www.daisy.org/z3986/2005/ncx/\" version=\"2005-1\">");

            //ncx/head
            tocBuilder.AppendLine("<head>");

            //ncx/head/meta(s)
            tocBuilder.AppendFormat("<meta name=\"dtb:uid\" content=\"{0}\"/>", options.ID);
            tocBuilder.AppendFormat("<meta name=\"dtb:depth\" content=\"{0}\"/>", 1);
            tocBuilder.AppendFormat("<meta name=\"dtb:totalPageCount\" content=\"{0}\"/>", 0);
            tocBuilder.AppendFormat("<meta name=\"dtb:maxPageNumber\" content=\"{0}\"/>", 0);
            //end /ncx/head/meta(s)

            //end /ncx/head
            tocBuilder.AppendLine("</head>");

            //ncx/docTitle
            tocBuilder.AppendLine("<docTitle>");

            //ncx/docTitle/text & end
            tocBuilder.AppendFormat("<text>{0}</text>", options.Title);

            //end /ncx/docTitle/text
            tocBuilder.AppendLine("</docTitle>");

            //ncx/navMap
            tocBuilder.AppendLine("<navMap>");


            //ncx/navMap/navPoint(s)
            //title page
            tocBuilder.AppendLine(GenerateNavPoint("Title Page", 1, "title_page.xhtml"));
            foreach (Chapter chapter in options.Chapters)
            {
                //chapters
                //need the +1 because the title page isn't included in the chapter list
                tocBuilder.AppendLine(GenerateNavPoint(chapter.ChapterTitle, chapter.ChapterNumber + 1, chapter.EmbeddedTitle));
            }
            //end /ncx/navMap/navPoint(s)

            //end /ncx/navMap
            tocBuilder.AppendLine("</navMap>");
            //end /ncx
            tocBuilder.AppendLine("</ncx>");

            //return the xml in ASCII encoding
            return(Encoding.ASCII.GetBytes(tocBuilder.ToString()));
        }
Example #4
0
        public static void GenerateEPUB(string inputFile, string outputFile, ConversionOptions options)
        {
            ZipFile file = new ZipFile(outputFile);

            //generate mimetype
            using (Stream mime = new MemoryStream())
            {
                byte[] mimeBytes = System.Text.Encoding.ASCII.GetBytes(_mimetype);
                mime.Write(mimeBytes, 0, mimeBytes.Length);
                file.AddEntry("mimetype", mime);
            }

            //generate META-INF
            file.AddDirectoryByName("META-INF");
            {
                //container.xml
                using (Stream container = new MemoryStream())
                {
                    byte[] containerBytes = System.Text.Encoding.ASCII.GetBytes(Crabwise.PDFtoEPUB.Properties.Resources.container);
                    container.Write(containerBytes, 0, containerBytes.Length);
                    file.AddEntry("META-INF\\container.xml", container);
                }
            }
            //generate OEBPS
            file.AddDirectoryByName("OEBPS");
            {
                //images
                file.AddDirectoryByName("OEBPS\\IMAGES");
                {
                    //add each image to the directory
                }

                //xhtml chapters


                //page-template.xpgt
                byte[] pagetemplateBytes = System.Text.Encoding.ASCII.GetBytes(Properties.Resources.page_template);
                file.AddEntry("OEBPS\\page-template.xpgt", pagetemplateBytes);

                //stylesheet.css
                byte[] stylesheetBytes = System.Text.Encoding.ASCII.GetBytes(Properties.Resources.stylesheet);
                file.AddEntry("OEBPS\\stylesheet.css", stylesheetBytes);

                //title_page.xhtml
                file.AddEntry("OEBPS\\title_page.xhtml", TitlePageGenerator.GetTitlePage(options));

                //Content.opf
                file.AddEntry("OEBPS\\content.opf", ContentFileGenerator.GetContentFile(options));

                //toc.ncx
                file.AddEntry("OEBPS\\toc.ncx", TOCFileGenerator.GetTOCFile(options));
            }
        }
        public static void GenerateEPUB(string inputFile, string outputFile, ConversionOptions options)
        {
            ZipFile file = new ZipFile(outputFile);

            //generate mimetype
            using (Stream mime = new MemoryStream())
            {
                byte[] mimeBytes = System.Text.Encoding.ASCII.GetBytes(_mimetype);
                mime.Write(mimeBytes, 0, mimeBytes.Length);
                file.AddEntry("mimetype", mime);
            }

            //generate META-INF
            file.AddDirectoryByName("META-INF");
            {
                //container.xml
                using (Stream container = new MemoryStream())
                {
                    byte[] containerBytes = System.Text.Encoding.ASCII.GetBytes(Crabwise.PDFtoEPUB.Properties.Resources.container);
                    container.Write(containerBytes, 0, containerBytes.Length);
                    file.AddEntry("META-INF\\container.xml", container);
                }
            }
            //generate OEBPS
            file.AddDirectoryByName("OEBPS");
            {
                //images
                file.AddDirectoryByName("OEBPS\\IMAGES");
                {
                    //add each image to the directory
                }

                //xhtml chapters

                //page-template.xpgt
                byte[] pagetemplateBytes = System.Text.Encoding.ASCII.GetBytes(Properties.Resources.page_template);
                file.AddEntry("OEBPS\\page-template.xpgt", pagetemplateBytes);

                //stylesheet.css
                byte[] stylesheetBytes = System.Text.Encoding.ASCII.GetBytes(Properties.Resources.stylesheet);
                file.AddEntry("OEBPS\\stylesheet.css", stylesheetBytes);

                //title_page.xhtml
                file.AddEntry("OEBPS\\title_page.xhtml", TitlePageGenerator.GetTitlePage(options));

                //Content.opf
                file.AddEntry("OEBPS\\content.opf", ContentFileGenerator.GetContentFile(options));

                //toc.ncx
                file.AddEntry("OEBPS\\toc.ncx", TOCFileGenerator.GetTOCFile(options));

            }
        }
        public static byte[] GetTOCFile(ConversionOptions options)
        {
            StringBuilder tocBuilder = new StringBuilder();

            tocBuilder.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

            //ncx
            tocBuilder.AppendLine("<ncx xmlns=\"http://www.daisy.org/z3986/2005/ncx/\" version=\"2005-1\">");

            //ncx/head
            tocBuilder.AppendLine("<head>");

            //ncx/head/meta(s)
            tocBuilder.AppendFormat("<meta name=\"dtb:uid\" content=\"{0}\"/>", options.ID);
            tocBuilder.AppendFormat("<meta name=\"dtb:depth\" content=\"{0}\"/>", 1);
            tocBuilder.AppendFormat("<meta name=\"dtb:totalPageCount\" content=\"{0}\"/>", 0);
            tocBuilder.AppendFormat("<meta name=\"dtb:maxPageNumber\" content=\"{0}\"/>", 0);
            //end /ncx/head/meta(s)

            //end /ncx/head
            tocBuilder.AppendLine("</head>");

            //ncx/docTitle
            tocBuilder.AppendLine("<docTitle>");

            //ncx/docTitle/text & end
            tocBuilder.AppendFormat("<text>{0}</text>", options.Title);

            //end /ncx/docTitle/text
            tocBuilder.AppendLine("</docTitle>");

            //ncx/navMap
            tocBuilder.AppendLine("<navMap>");

            //ncx/navMap/navPoint(s)
            //title page
            tocBuilder.AppendLine(GenerateNavPoint("Title Page", 1, "title_page.xhtml"));
            foreach (Chapter chapter in options.Chapters)
            {
                //chapters
                //need the +1 because the title page isn't included in the chapter list
                tocBuilder.AppendLine(GenerateNavPoint(chapter.ChapterTitle, chapter.ChapterNumber + 1, chapter.EmbeddedTitle));
            }
            //end /ncx/navMap/navPoint(s)

            //end /ncx/navMap
            tocBuilder.AppendLine("</navMap>");
            //end /ncx
            tocBuilder.AppendLine("</ncx>");

            //return the xml in ASCII encoding
            return Encoding.ASCII.GetBytes(tocBuilder.ToString());
        }
        public static ConversionOptions FindMetaData(string html)
        {
            ConversionOptions returnable = new ConversionOptions();

            returnable.Title  = ParseTag(html, "Title");
            returnable.Author = ParseMeta(html, "author");
            DateTime publish = DateTime.MinValue;

            DateTime.TryParse(ParseMeta(html, "date"), out publish);
            returnable.PublishDate = publish;

            return(returnable);
        }
Example #8
0
        public static byte[] GetTitlePage(ConversionOptions options)
        {
            StringBuilder titleBuilder = new StringBuilder();

            titleBuilder.AppendLine("<?xml version=\"1.0\" encoding=\"iso-8859\"?>");
            titleBuilder.AppendLine("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
            titleBuilder.AppendLine("<html xmlns=\"http://www.w3.org/1999/xhtml\">");

            titleBuilder.AppendLine("<head>");

            titleBuilder.AppendLine("<title>");
            titleBuilder.Append(options.Title);
            titleBuilder.Append("</title>");

            titleBuilder.AppendLine("<link href=\"stylesheet.css\" type=\"text/css\" rel=\"stylesheet\" />");
            titleBuilder.AppendLine("<link rel=\"stylesheet\" type=\"application/vnd.adobe-page-template+xml\" href=\"page-template.xpgt\"/>");

            titleBuilder.AppendLine("</head>");

            titleBuilder.AppendLine("<body>");
            titleBuilder.AppendLine("<div>");
            titleBuilder.AppendLine("<p>");
            titleBuilder.Append("&nbsp");
            titleBuilder.Append("</p>");

            titleBuilder.AppendLine("<h2>");
            titleBuilder.Append(options.Title);
            titleBuilder.Append("</h2>");

            titleBuilder.AppendLine("<p>&nbsp</p>");

            titleBuilder.AppendLine("<h3>");
            titleBuilder.Append(options.Author);
            titleBuilder.Append("</h3>");

            titleBuilder.AppendLine("<p>&nbsp</p>");
            titleBuilder.AppendLine("<h7>Generated by PDFtoEPUB (www.crabwisestudios.com/PDFtoEPUB/Overview)</h7>");

            titleBuilder.AppendLine("</div></body></html>");

            byte[] titleBytes = Encoding.ASCII.GetBytes(titleBuilder.ToString());

            return(titleBytes);
        }
        public static byte[] GetContentFile(ConversionOptions options)
        {
            StringBuilder contentBuilder = new StringBuilder();

            contentBuilder.AppendLine("<?xml version=\"1.0\"?>");

            //package
            contentBuilder.AppendFormat("<package version=\"2.0\" xmlns=\"http://www.idpf.org/2007/opf\" unique-identifier=\"{0}\">", options.ID);

            //package/metadata
            contentBuilder.AppendLine("<metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:opf=\"http://www.idpf.org/2007/opf\">");
            //package/metadata/dc:title & end
            contentBuilder.AppendFormat("<dc:title>{0}</dc:title>", options.Title);
            //package/metadata/dc:creator & end
            contentBuilder.AppendFormat("<dc:creator opf:role=\"aut\">{0}</dc:creator>", options.Author);
            //package/metadata/dc:language & end
            contentBuilder.AppendLine("<dc:language>en-US</dc:language>"); //assuming en-US
            //package/metadata/dc:rights & end
            contentBuilder.AppendLine("<dc:rights>Public Domain</dc:rights>");
            //package/metadata/dc:publishers & end
            contentBuilder.AppendLine("<dc:publisher>Crabwise Studios</dc:publisher>");
            //package/metadata/dc:identifier & end
            contentBuilder.AppendFormat("<dc:identifier id=\"BookId\">urn:uuid:{0}</dc:identifier>", options.ID);
            //end /package/metadata
            contentBuilder.AppendLine("</metadata>");

            //package/manifest
            contentBuilder.AppendLine("<manifest>");

            //package/manifest/item(s)

            //table of contents file
            contentBuilder.AppendLine(GenerateItem("ncx", "toc.ncx", "text-xml"));
            //main stylesheet
            contentBuilder.AppendLine(GenerateItem("style", "stylesheet.css", "text/css"));
            //adobe digital editions page template
            contentBuilder.AppendLine(GenerateItem("pagetemplate", "page-template.xpgt", "application/vnd.adobe-page-template+xml"));
            //title page
            contentBuilder.AppendLine(GenerateItem("titlepage", "title_page.xhtml", "application/xhtml+xml"));

            //add the chapters to the manifest
            foreach (Chapter chapter in options.Chapters)
            {
                //example id: chapter01, chapter19
                contentBuilder.AppendLine(GenerateItem("chapter" + chapter.ChapterNumber.ToString().PadLeft(2, '0'), chapter.EmbeddedTitle, "application/xhtml+xml"));
            }

            //add the images to the manifest
            for (int imgi = 0; imgi < options.Images.Count; imgi++)
            {
                //example id: img001, img019, img456
                contentBuilder.AppendLine(GenerateItem("img" + imgi.ToString().PadLeft(3, '0'), options.Images[imgi], "imge/jpeg"));
            }

            //end /package/manifest
            contentBuilder.AppendLine("</manifest>");

            //package/spine
            contentBuilder.AppendLine("<spine toc=\"ncx\">");

            //package/spine/itemref(s)
            //add the title page to the spine
            contentBuilder.AppendLine(GenerateItemRef("titlepage"));

            //add the chapters to the spine
            foreach (Chapter chapter in options.Chapters)
            {
                //example same as above
                contentBuilder.AppendLine(GenerateItemRef("chapter" + chapter.ChapterNumber.ToString().PadLeft(2, '0')));
            }
            //end /package/spine
            contentBuilder.AppendLine("</spine>");

            //end /package
            contentBuilder.AppendLine("</package>");

            //return the xml in ASCII encoding
            return(Encoding.ASCII.GetBytes(contentBuilder.ToString()));
        }
        public static byte[] GetContentFile(ConversionOptions options)
        {
            StringBuilder contentBuilder = new StringBuilder();

            contentBuilder.AppendLine("<?xml version=\"1.0\"?>");

            //package
            contentBuilder.AppendFormat("<package version=\"2.0\" xmlns=\"http://www.idpf.org/2007/opf\" unique-identifier=\"{0}\">", options.ID);

            //package/metadata
            contentBuilder.AppendLine("<metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:opf=\"http://www.idpf.org/2007/opf\">");
            //package/metadata/dc:title & end
            contentBuilder.AppendFormat("<dc:title>{0}</dc:title>", options.Title);
            //package/metadata/dc:creator & end
            contentBuilder.AppendFormat("<dc:creator opf:role=\"aut\">{0}</dc:creator>", options.Author);
            //package/metadata/dc:language & end
            contentBuilder.AppendLine("<dc:language>en-US</dc:language>"); //assuming en-US
            //package/metadata/dc:rights & end
            contentBuilder.AppendLine("<dc:rights>Public Domain</dc:rights>");
            //package/metadata/dc:publishers & end
            contentBuilder.AppendLine("<dc:publisher>Crabwise Studios</dc:publisher>");
            //package/metadata/dc:identifier & end
            contentBuilder.AppendFormat("<dc:identifier id=\"BookId\">urn:uuid:{0}</dc:identifier>", options.ID);
            //end /package/metadata
            contentBuilder.AppendLine("</metadata>");

            //package/manifest
            contentBuilder.AppendLine("<manifest>");

            //package/manifest/item(s)

            //table of contents file
            contentBuilder.AppendLine(GenerateItem("ncx", "toc.ncx", "text-xml"));
            //main stylesheet
            contentBuilder.AppendLine(GenerateItem("style", "stylesheet.css", "text/css"));
            //adobe digital editions page template
            contentBuilder.AppendLine(GenerateItem("pagetemplate", "page-template.xpgt", "application/vnd.adobe-page-template+xml"));
            //title page
            contentBuilder.AppendLine(GenerateItem("titlepage", "title_page.xhtml", "application/xhtml+xml"));

            //add the chapters to the manifest
            foreach (Chapter chapter in options.Chapters)
            {
                //example id: chapter01, chapter19
                contentBuilder.AppendLine(GenerateItem("chapter" + chapter.ChapterNumber.ToString().PadLeft(2, '0'), chapter.EmbeddedTitle, "application/xhtml+xml"));
            }

            //add the images to the manifest
            for (int imgi = 0; imgi < options.Images.Count; imgi++)
            {
                //example id: img001, img019, img456
                contentBuilder.AppendLine(GenerateItem("img" + imgi.ToString().PadLeft(3, '0'), options.Images[imgi], "imge/jpeg"));
            }

            //end /package/manifest
            contentBuilder.AppendLine("</manifest>");

            //package/spine
            contentBuilder.AppendLine("<spine toc=\"ncx\">");

            //package/spine/itemref(s)
            //add the title page to the spine
            contentBuilder.AppendLine(GenerateItemRef("titlepage"));

            //add the chapters to the spine
            foreach (Chapter chapter in options.Chapters)
            {
                //example same as above
                contentBuilder.AppendLine(GenerateItemRef("chapter" + chapter.ChapterNumber.ToString().PadLeft(2, '0')));
            }
            //end /package/spine
            contentBuilder.AppendLine("</spine>");

            //end /package
            contentBuilder.AppendLine("</package>");

            //return the xml in ASCII encoding
            return Encoding.ASCII.GetBytes(contentBuilder.ToString());
        }