Java Library for EPUB Documents Manipulation
Open Source Java API to Create, Read & Manipulate EPUB Files from your own applications.
Epublib is a very useful open source Java library that enables software developers to work with EPUB files inside Java applications. It fully supports reading, writing, and manipulating EPUB files with ease. A file with .epub extension is an e-book file format that offers a standard digital publication format for publishers and users. The library can be used to programmatically read and write EPUB files as well as from the command-line tool.
The Epublib library is very simply designed but at the same time it is also possible to achieve complex tasks with ease. The library consists of two parts, the core and a collection of the tool. The tools incorporated several important tools such as a cleanup tool for EPUB, Generating EPUBs from HTML files, Generating an EPUB from an uncompressed HTML file, and a swing-based EPUB viewer tool is also part of the library.
The library also supports converting (uncompressed) windows help (.chm) files to EPUBs. Once the windows help file is uncompressed with a tool like chmlib, the Epublib library can easily generate an EPUB file out of the resultant HTML and windows help index files.
Getting Started with Epublib
To include epublib in a maven build do the following:
Add this to your set of repositories:
<repositories>
<repository>
<id>psiegman-repo</id>
<url>https://github.com/psiegman/mvn-repo/raw/master/releases</url>
</repositories>
</repositories>
Maven Dependency - Add the following to your pom.xml:
<dependency>
<groupId>nl.siegmann.epublib</groupId>
<artifactId>epublib-core</artifactId>
<version>3.1</version>
</dependency>
You can also install it manually; download the latest release files directly from GitHub repository.
Create & Edit EPUB Files via Java Library
The open source Epublib library allows software developers to create new EPUB documents inside their own Java applications with ease. Once the file is created you can set the title of your choice, add an author, set cover image, apply CSS styles, add chapters and cover image for the chapter, add section to the file, Create Epub-Writer and so on. The same file can also be created in Android.
Create Simple EPUB Book via Java Library
package nl.siegmann.epublib.examples;
package nl.siegmann.epublib.epub;
import java.io.FileOutputStream;
import nl.siegmann.epublib.domain.Author;
import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.domain.Resource;
import nl.siegmann.epublib.domain.TOCReference;
public class Simple1 {
public static void main(String[] args) {
try {
// Create new Book
Book book = new Book();
// Set the title
book.getMetadata().addTitle("Epublib test book 1");
// Add an Author
book.getMetadata().addAuthor(new Author("Joe", "Tester"));
// Set cover image
book.getMetadata().setCoverImage(new Resource(Simple1.class.getResourceAsStream("/book1/test_cover.png"), "cover.png"));
// Add Chapter 1
book.addSection("Introduction", new Resource(Simple1.class.getResourceAsStream("/book1/chapter1.html"), "chapter1.html"));
// Add css file
book.getResources().add(new Resource(Simple1.class.getResourceAsStream("/book1/book1.css"), "book1.css"));
// Add Chapter 2
TOCReference chapter2 = book.addSection("Second Chapter", new Resource(Simple1.class.getResourceAsStream("/book1/chapter2.html"), "chapter2.html"));
// Add image used by Chapter 2
book.getResources().add(new Resource(Simple1.class.getResourceAsStream("/book1/flowers_320x240.jpg"), "flowers.jpg"));
// Add Chapter2, Section 1
book.addSection(chapter2, "Chapter 2, section 1", new Resource(Simple1.class.getResourceAsStream("/book1/chapter2_1.html"), "chapter2_1.html"));
// Add Chapter 3
book.addSection("Conclusion", new Resource(Simple1.class.getResourceAsStream("/book1/chapter3.html"), "chapter3.html"));
// Create EpubWriter
EpubWriter epubWriter = new EpubWriter();
// Write the Book as Epub
epubWriter.write(book, new FileOutputStream("test1_book1.epub"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
How to Read Existing EPUB via Java Library?
The open source Epublib library has included complete functionality for opening an existing EPUB documents and reading it inside their own Java applications. First you need to load the book from the Input-stream, then can load the titles and subtitles, table of contents, author of the book, cover image of the EPUB book if any and so on. It is also possible to read line by line of the content of the document.
How to Read EPUB File using Java API?
// read epub file
EpubReader epubReader = new EpubReader();
Book book = epubReader.readEpub(new FileInputStream(“mybook.epub”));
// print the first title
List titles = book.getMetadata().getTitles();
System.out.println(“book title:” + (titles.isEmpty() ? “book has no title” : titles.get(0)));