How to edit Mobi file

Introduction

Mobi format is an E-Book format, developed by the French company MobiPocket and is based on XML. E-Books in this format can contain text with rich formatting, images, and different annotations like bookmarks, notes, highlights, corrections and so on. Mobi books can have a DRM protection.

Starting from the version 20.7, the GroupDocs.Editor for .NET is able to open (load) Mobi documents for editing. However, at this moment a saving into Mobi is not available, so users should choose some other format (the most likely AZW3 or ePub) in order to save edited Mobi document.

Starting from the version 22.7 the GroupDocs.Editor is able to edit the e-books in AZW3 format, also known as Kindle Format 8 (KF8), whech may be considered as a successor to the Mobi. Unlike the MOBI, the AZW3 are supported on import (since version 22.7) and export (since version 22.9).

Loading Mobi file for edit

Despite of a distinct format, which doesn’t belong to any of existing format families, Mobi has no dedicated loading options. So for loading it into the instance of the Editor class, users should simply specify a path to the Mobi file or a stream with its content in the constructor:

string inputMobiPath = "C://input/book.mobi";
Editor editorFromPath = new Editor(inputMobiPath);
  
FileStream inputMobiStream = File.OpenRead("C://input/book.mobi");
Editor editorFromStream = new Editor(delegate() { return inputMobiStream; });

There is no loading options, because Mobi has nothing to tune-up during loading, — it cannot have a password protection, and can be processed only in one way.

Editing Mobi file

Because Mobi belongs to the e-Book format family, it utilize common edit options for all e-Book formats — an EbookEditOptions. This class may be described as a truncated version of a WordProcessingEditOptions class, because EbookEditOptions contains a subset of options from WordProcessingEditOptionsEnablePagination and EnableLanguageInformation and, as in the WordProcessingEditOptions, they are disabled (false) by default:

public bool EnablePagination { get; set; }
  
public bool EnableLanguageInformation { get; set; }

They have exactly the same meaning as their “siblings” from WordProcessingEditOptions. EnablePagination allows to switch between float and paginal mode in the resultant HTML document. EnableLanguageInformation allows to enable exporting language information in HTML. This is very useful for books, which have parts of text, written on different languages.

Example of usage is below (let’s assume that Editor instance with loaded Mobi document is already created):

Options.EbookEditOptions editOptions = new Options.EbookEditOptions();
editOptions.EnablePagination = true;
editOptions.EnableLanguageInformation = true;
using (EditableDocument opened = editor.Edit(editOptions))
{
    //save it or pass to the WYSIWYG-editor
}
Note
The EbookEditOptions class was firstly introduced in the GroupDocs.Editor for .NET version 22.9. However, prior this version, there was a class MobiEditOptions, which was designed especially for the Mobi format. In the 22.9 version of GroupDocs.Editor for .NET this MobiEditOptions class was removed in favor to the more generalized EbookEditOptions, so when updating from the 22.7 or older versions to the 22.9 or newer, please update the class name accordingly. All class members in these both classes are identical.

Save Mobi file after edit

As it was stated in the introduction, for now saving edited document in Mobi format is not supported.

Detecting Mobi file

As for documents of all supporting types, Mobi documents can be detected using a GetDocumentInfo() method of the Editor class. In case when a valid Mobi document was loaded into the Editor instance, a GetDocumentInfo() will return an instance of a Metadata.EbookDocumentInfo class, which inherits from IDocumentInfo interface, which, in turn, defines 4 properties: Format, PageCount, Size, and IsEncrypted.

  • Format property returns a Formats.EBookFormats struct, which for the files with a *.mobi extension can have a Mobi or Azw3 value.
  • PageCount property returns an approximate number of pages in case of MOBI or AZW3 or a number of chapters in case of ePub. For the Mobi, it is approximate, because Mobi format internally is a set of HTML documents (chapters), which are not separated on pages and even have no strict page dimensions, which allows to split content on page blocks and thus calculate the number of pages. This decision was made by Mobi format designers intentionally to allows variable page size (and count) on different devices — from FullHD displays to smartphones. So, for returning a page count for a Mobi document, GroupDocs.Editor assumes standard A4 page size in a portrait orientation, splits existing document content on such “papers”, and then calculates its count. So the returning number should be treated very carefully and approximately, users should not rely on it.
  • Size property returns a number of bytes of a Mobi document.
  • IsEncrypted property always returns a false value, because Mobi documents cannot be encrypted with password, like PDF or Office Open XML.