Working with metadata in FB2 e-books

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Metadata MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-metadata -- dnx GroupDocs.Metadata.Mcp --yes
Folders, licensing and more →

What is FB2?

FictionBook 2 (FB2) is an XML-based e-book format. Its metadata is stored in the <description> element at the beginning of the file and is divided into several sections.

GroupDocs.Metadata for Java reads these sections through the Fb2Package class:

SectionGetterContents
Title infogetTitleInfo()Book title, authors, translators, genres, keywords, date, language, source language, sequence
Source title infogetSrcTitleInfo()The same fields for the original book, if this book is a translation
Document infogetDocumentInfo()Authors of the FB2 file, program used, date, source URL, ID and version
Publish infogetPublishInfo()Book name, publisher, city, year, ISBN and sequence of the paper edition
Custom infogetCustomInfo()Any additional information

The FB2 format is supported for reading only.

Reading FB2 metadata

  1. Load an FB2 e-book
  2. Get the root metadata package
  3. Extract the native metadata package using the Fb2RootPackage.getFb2Package method
  4. Read the sections you need. The optional sections can be null.
try (Metadata metadata = new Metadata(Constants.InputFb2)) {
    Fb2RootPackage root = metadata.getRootPackageGeneric();
    Fb2Package fb2 = root.getFb2Package();

    Fb2TitleInfo titleInfo = fb2.getTitleInfo();
    System.out.println(titleInfo.getBookTitle());
    System.out.println(titleInfo.getLang());
    System.out.println(titleInfo.getKeywords());
    for (Fb2Author author : titleInfo.getAuthors()) {
        System.out.println(String.format("%s %s", author.getFirstName(), author.getLastName()));
    }
    for (Fb2Genre genre : titleInfo.getGenres()) {
        System.out.println(genre);
    }

    if (fb2.getDocumentInfo() != null) {
        System.out.println(fb2.getDocumentInfo().getProgramUsed());
        System.out.println(fb2.getDocumentInfo().getDate());
        System.out.println(fb2.getDocumentInfo().getId());
    }

    if (fb2.getPublishInfo() != null) {
        System.out.println(fb2.getPublishInfo().getPublisher());
        System.out.println(fb2.getPublishInfo().getYear());
        System.out.println(fb2.getPublishInfo().getIsbn());
    }
}

Genres are returned as Fb2Genre enumeration values that match the genre codes of the FB2 specification, for example sf_space or prose_classic.

More resources

GitHub examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free online document metadata management App

Along with full featured Java library we provide simple, but powerful free Apps.

You are welcome to view and edit metadata of PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, emails, images and more with our free online Free Online Document Metadata Viewing and Editing App.