FPDF_GetPageCount

FPDF_GetPageCount(document)

Description

Gets the number of pages in a PDF document. This function is typically called after loading a document to determine how many pages are available for rendering or processing.

Prerequisites

This example uses the initializePdfium helper function from our Getting Started guide. Make sure to include this function in your code before trying the examples below.

Parameters

NameTypeDescription
documentnumberA document handle obtained from FPDF_LoadMemDocument or other document loading functions.

Return Value

Returns an integer representing the number of pages in the document. If the document handle is invalid, the function returns 0.

Interactive Example

Here’s a simple example that loads a PDF and displays its page count:

PREVIEW:Get PDF Page Count

Example

// Note: The initializePdfium function is a helper that initializes the PDFium library. // For the full implementation, see: /docs/pdfium/getting-started import { initializePdfium } from './initialize-pdfium'; async function getPdfPageCount(pdfData: Uint8Array) { // Step 1: Initialize PDFium const pdfium = await initializePdfium(); // Step 2: Load the PDF document const filePtr = pdfium.pdfium.wasmExports.malloc(pdfData.length); pdfium.pdfium.HEAPU8.set(pdfData, filePtr); const docPtr = pdfium.FPDF_LoadMemDocument(filePtr, pdfData.length, 0); if (!docPtr) { const error = pdfium.FPDF_GetLastError(); pdfium.pdfium.wasmExports.free(filePtr); throw new Error(`Failed to load PDF: ${error}`); } try { // Step 3: Get the page count const pageCount = pdfium.FPDF_GetPageCount(docPtr); return pageCount; } finally { // Step 4: Clean up pdfium.FPDF_CloseDocument(docPtr); pdfium.pdfium.wasmExports.free(filePtr); } } // Usage fetch('sample.pdf') .then(response => response.arrayBuffer()) .then(buffer => getPdfPageCount(new Uint8Array(buffer))) .then(pageCount => { console.log(`The PDF has ${pageCount} pages`); }) .catch(error => console.error(error));

Best Practices

  1. Call early in your workflow: Call FPDF_GetPageCount early in your PDF processing workflow to determine how many pages you need to handle. This helps in planning resource allocation and progress reporting.

  2. Check for valid document: Ensure the document handle is valid before calling this function. If FPDF_LoadMemDocument or other document loading functions return 0, don’t attempt to call FPDF_GetPageCount.

  3. Validate user inputs: When accepting user input for page navigation, use the page count to validate that the requested page is within the valid range (0 to pageCount - 1).

  4. Progress reporting: For large documents, use the page count to provide accurate progress indicators to users during operations that process all pages.

Common Issues

  1. Invalid document handle: If you pass an invalid document handle to this function, it will return 0, which could be misinterpreted as a valid document with no pages. Always check that your document was loaded successfully before calling this function.

  2. Performance with large documents: For very large documents, you may want to avoid operations that require iterating through all pages at once. Consider processing pages in batches for better performance and memory usage.

Last updated on March 31, 2025