8.5 Getting Pages from a document

Once you have a laid out document, you presumably want to be able to do something with it. The first thing to know is how many pages it contains. This is achieved by calling fz_count_pages:

/* 
   fz_count_pages: Return the number of pages in document 
 
   May return 0 for documents with no pages. 
*/ 
int fz_count_pages(fz_context *ctx, fz_document *doc);

For document types like images, they appear as a single page. If you forget to lay out a reflowable document, this will trigger a layout for a default size and return the required number of pages.

Once you know how many pages there are, you can fetch the fz_page object for each page required:

/* 
   fz_load_page: Load a page. 
 
   After fz_load_page is it possible to retrieve the size of the 
   page using fz_bound_page, or to render the page using 
   fz_run_page_*. Free the page by calling fz_drop_page. 
 
   number: page number, 0 is the first page of the document. 
*/ 
fz_page *fz_load_page(fz_context *ctx, fz_document *doc, int number);

The pages of a document with n pages are numbered from 0 to n-1.

In common with most other object types, fz_pages are reference counted:

/* 
   fz_keep_page: Keep a reference to a loaded page. 
 
   Does not throw exceptions. 
*/ 
fz_page *fz_keep_page(fz_context *ctx, fz_page *page); 
 
/* 
   fz_drop_page: Free a loaded page. 
 
   Does not throw exceptions. 
*/ 
void fz_drop_page(fz_context *ctx, fz_page *page);

Once the last reference to a page is dropped, the resources it consumes are all released automatically.