예제 #1
0
        public void Archive(IEnumerable<FileInfo> files, string epubPath)
        {
            Book book = new Book();
            book.Language = "ja";
            int chapterNumber = 1;
            foreach (var file in files)
            {
                try
                {
                    using (StreamReader sr = new StreamReader(file.FullName, System.Text.Encoding.UTF8))
                    {
                        string contents = sr.ReadToEnd();
                        string title = GetTitle(contents);
                        // For redirect case.
                        title = title == "" ? Path.GetFileNameWithoutExtension(file.Name) : title;
                        Chapter chapter1 = new Chapter()
                        {
                            Title = title,

                            //You are responsible for setting chapter numbers appropriately, starting with 1.
                            Number = chapterNumber,

                            //Be sure your Content is XHTML 1.1 compliant!
                            Content = @"<?xml version=""1.0"" encoding=""UTF-8""?>
            <!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.1//EN"" ""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"">
            " + contents
                        };
                        book.FileItems.Add(chapter1);
                    }
                    chapterNumber++;
                }
                catch (FileNotFoundException)
                {
                    // in some Greeth filename, StreamReader return file not found exception.
                    // Just ignore it.
                }
            }
            book.Save(epubPath);
        }
예제 #2
0
        public void TestHelloEPub()
        {
            Book book = new Book();
            book.Language = "ja";
            //You can also use the factory, FileItem.Create(FileItemType.XHTML).
            //For images, you must use the factory, FileItem.Create(FileItemType.JPEG), etc
            Chapter chapter1 = new Chapter()
            {
                Title = "Chapter 1!",

                //You are responsible for setting chapter numbers appropriately, starting with 1.
                Number = 1,

                //Be sure your Content is XHTML 1.1 compliant!
                /*
                Content = @"<?xml version=""1.0"" encoding=""UTF-8""?>
            <!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.1//EN"" ""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"">
            <html><head><title>Chapter 1</title></head>
            <body><h1>Episode 1</h1><h2>The Phantom Menace</h2>
            <p>Well look here, it's content in an epub!</p></body></html>"
                 * */
                Content = _content
            };

            //Book.FileItems holds all of the files in the package: chapters, images, CSS, etc.
            //They all inherit from the abstract type FileItem and so it's easily extensible.
            book.FileItems.Add(chapter1);

            //Repeat as necessary for each chapter
            //And then save to a file.
            //EPubLib will create a table of contents with an entry for each chapter,
            //and then package/zip everything into the proper format.
            book.Save(@"AHeaderTDCloseDblquote.epub");
        }