8.2 Opening/Closing a document

The simplest way to load a document is to load it from the local filing system:

/* 
   fz_open_document: Open a PDF, XPS or CBZ document. 
 
   Open a document file and read its basic structure so pages and 
   objects can be located. MuPDF will try to repair broken 
   documents (without actually changing the file contents). 
 
   The returned fz_document is used when calling most other 
   document related functions. 
 
   filename: a path to a file as it would be given to open(2). 
*/ 
fz_document *fz_open_document(fz_context *ctx, const char *filename);

For embedded systems, or secure applications, the use of a local filing system may be inappropriate, so an alternative is available whereby documents can be opened from a fz_stream. See chapter 12 The Stream interface for more details on fz_streams.

/* 
   fz_open_document_with_stream: Open a PDF, XPS or CBZ document. 
 
   Open a document using the specified stream object rather than 
   opening a file on disk. 
 
   magic: a string used to detect document type; either a file name or mime-type. 
*/ 
fz_document *fz_open_document_with_stream(fz_context *ctx, const char *magic, fz_stream *stream);

Almost any data source can be wrapped up as a fz_stream; see chapter 12 The Stream interface for more details.

In common with most other objects in MuPDF, fz_documents are reference counted:

/* 
   fz_keep_document: Keep a reference to an open document. 
 
   Does not throw exceptions. 
*/ 
fz_document *fz_keep_document(fz_context *ctx, fz_document *doc); 
 
/* 
   fz_drop_document: Release an open document. 
 
   The resource store in the context associated with fz_document 
   is emptied, and any allocations for the document are freed when 
   the last reference is dropped. 
 
   Does not throw exceptions. 
*/ 
void fz_drop_document(fz_context *ctx, fz_document *doc);

Once the last reference to the document is dropped, all resources used by that document will be released, including those in the Store.