Extract text from scanned documents and images, create searchable PDFs, and make your content accessible with powerful OCR capabilities.
The Text Extraction API uses Optical Character Recognition (OCR) to extract text from scanned documents, images, and non-searchable PDFs. This powerful feature converts visual text content into machine-readable text that can be searched, copied, and analyzed.
POST https://api.mega-pdf.com/api/pdf/extract-text
Authenticate requests using an API key in the x-api-key
header.
// Header example
x-api-key: your-api-key
The API accepts multipart/form-data
requests with the following parameters:
Parameter | Type | Description | Required |
---|---|---|---|
file | File | PDF file or image to extract text from (max 50MB) | Yes |
language | String | OCR language code (e.g., 'eng' for English, 'fra' for French) | No (default: eng) |
outputFormat | String | Output format: 'txt', 'json', 'xml', or 'html' | No (default: txt) |
enhanceImages | Boolean | Preprocess images to improve OCR accuracy | No (default: false) |
Extract text from a scanned PDF using cURL:
curl -X POST https://api.mega-pdf.com/api/pdf/extract-text \
-H "x-api-key: your-api-key" \
-F "file=@/path/to/scanned-document.pdf" \
-F "language=eng" \
-F "outputFormat=txt" \
-F "enhanceImages=true"
Successful responses include the extracted text content:
{
"success": true,
"message": "Text extracted successfully",
"fileUrl": "/api/file?folder=extracted&filename=uuid-extracted.txt",
"filename": "uuid-extracted.txt",
"originalName": "scanned-document.pdf",
"pageCount": 5,
"characterCount": 15230,
"detectedLanguage": "English",
"previewText": "This is a preview of the extracted text content...",
"billing": {
"usedFreeOperation": true,
"freeOperationsRemaining": 9,
"currentBalance": 10.50,
"operationCost": 0.00
}
}
For JSON output format:
{
"success": true,
"message": "Text extracted successfully",
"fileUrl": "/api/file?folder=extracted&filename=uuid-extracted.json",
"filename": "uuid-extracted.json",
"originalName": "scanned-document.pdf",
"pageCount": 5,
"data": {
"pages": [
{
"pageNumber": 1,
"text": "Content of page 1...",
"blocks": [
{
"text": "Block of text",
"bbox": [100, 200, 300, 250],
"confidence": 0.95
},
// more text blocks...
]
},
// more pages...
]
},
"billing": {
"usedFreeOperation": true,
"freeOperationsRemaining": 9,
"currentBalance": 10.50,
"operationCost": 0.00
}
}
Error responses:
{
"success": false,
"error": "Failed to extract text: The document appears to be password protected"
}
Using the Text Extraction API with JavaScript:
const formData = new FormData();
formData.append('file', fs.createReadStream('scanned-document.pdf'));
formData.append('language', 'eng');
formData.append('outputFormat', 'json');
formData.append('enhanceImages', 'true');
fetch('https://api.mega-pdf.com/api/pdf/extract-text', {
method: 'POST',
headers: {
'x-api-key': 'your-api-key'
},
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
console.log('Text extracted successfully');
console.log('Page count:', data.pageCount);
console.log('Character count:', data.characterCount);
console.log('Download URL:', data.fileUrl);
// If using JSON output format, you can access the structured data
if (data.data && data.data.pages) {
data.data.pages.forEach(page => {
console.log(`Page ${page.pageNumber} content: ${page.text.substring(0, 100)}...`);
});
}
} else {
console.error('Failed to extract text:', data.error);
}
})
.catch(error => console.error('Error:', error));