FPDFText_ClosePage
FPDFText_ClosePage(text_page)
Description
Closes a text page object and releases all resources associated with it. This function must be called when you’re done with a text page object that was created with FPDFText_LoadPage
.
Failing to call this function will result in memory leaks in your application.
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
Name | Type | Description |
---|---|---|
text_page | number | A text page handle obtained from FPDFText_LoadPage . |
Return Value
This function does not return a value.
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 extractTextFromPage(pdfData: Uint8Array, pageIndex: number): Promise<string> {
// Initialize PDFium
const pdfium = await initializePdfium();
// 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 {
// Load the page
const pagePtr = pdfium.FPDF_LoadPage(docPtr, pageIndex);
if (!pagePtr) {
throw new Error(`Failed to load page ${pageIndex}`);
}
try {
// Create a text page object
const textPagePtr = pdfium.FPDFText_LoadPage(pagePtr);
if (!textPagePtr) {
throw new Error(`Failed to load text for page ${pageIndex}`);
}
try {
// Perform text operations...
const charCount = pdfium.FPDFText_CountChars(textPagePtr);
if (charCount <= 0) {
return ''; // No text on this page
}
// Allocate a buffer for text
const bufferSize = (charCount + 1) * 2; // UTF-16, 2 bytes per character
const textBufferPtr = pdfium.pdfium.wasmExports.malloc(bufferSize);
try {
// Extract the text
const extractedLength = pdfium.FPDFText_GetText(
textPagePtr,
0,
charCount,
textBufferPtr
);
if (extractedLength > 0) {
return pdfium.pdfium.UTF16ToString(textBufferPtr);
}
return '';
} finally {
// Clean up text buffer
pdfium.pdfium.wasmExports.free(textBufferPtr);
}
} finally {
// Always close the text page when done
pdfium.FPDFText_ClosePage(textPagePtr);
}
} finally {
// Clean up page
pdfium.FPDF_ClosePage(pagePtr);
}
} finally {
// Clean up document
pdfium.FPDF_CloseDocument(docPtr);
pdfium.pdfium.wasmExports.free(filePtr);
}
}
// Usage
fetch('sample.pdf')
.then(response => response.arrayBuffer())
.then(buffer => extractTextFromPage(new Uint8Array(buffer), 0))
.then(text => console.log('Extracted text:', text))
.catch(error => console.error('Error:', error));
Best Practices
-
Always close text pages: Call
FPDFText_ClosePage
for every text page object you create withFPDFText_LoadPage
, even if operations on the text page fail. This prevents memory leaks. -
Use try/finally blocks: Place your text page operations inside a try block and the
FPDFText_ClosePage
call in a finally block to ensure the text page is closed even if exceptions occur:const textPagePtr = pdfium.FPDFText_LoadPage(pagePtr); try { // Perform text operations... } finally { pdfium.FPDFText_ClosePage(textPagePtr); }
-
Proper closing order: When working with multiple PDFium objects, close them in the reverse order of creation:
- First, close any text buffers with
pdfium.pdfium.wasmExports.free()
- Next, close the text page with
FPDFText_ClosePage()
- Then, close the PDF page with
FPDF_ClosePage()
- Finally, close the PDF document with
FPDF_CloseDocument()
- First, close any text buffers with
-
Don’t use the text page after closing: After calling
FPDFText_ClosePage
, the text page handle is no longer valid. Any attempts to use it will result in undefined behavior and likely crashes.
Common Issues
-
Memory leaks: The most common issue is forgetting to close text pages, especially in error cases. This leads to memory leaks that can eventually crash your application.
-
Invalid text page handles: Using a text page handle after it has been closed can cause crashes or other unexpected behavior.
-
Closing in the wrong order: For nested objects (e.g., text pages created from PDF pages), make sure to close them in the reverse order they were created.
Related Functions
- FPDFText_LoadPage - Create a text page object from a PDF page
- FPDFText_CountChars - Get the number of characters on a page
- FPDFText_GetText - Extract text from a page
- FPDF_LoadPage - Load a PDF page
- FPDF_ClosePage - Close a PDF page
- FPDF_CloseDocument - Close a PDF document