OCR API

Extract text from scanned documents and images, create searchable PDFs, and make your content accessible with powerful OCR capabilities.

Text Extraction API
Extract text from scanned documents and images using OCR technology

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.

Endpoint

POST https://api.mega-pdf.com/api/pdf/extract-text

Authentication

Authenticate requests using an API key in the x-api-key header.

// Header example
x-api-key: your-api-key

Request Parameters

The API accepts multipart/form-data requests with the following parameters:

ParameterTypeDescriptionRequired
fileFilePDF file or image to extract text from (max 50MB)Yes
languageStringOCR language code (e.g., 'eng' for English, 'fra' for French)No (default: eng)
outputFormatStringOutput format: 'txt', 'json', 'xml', or 'html'No (default: txt)
enhanceImagesBooleanPreprocess images to improve OCR accuracyNo (default: false)

Example Request

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"

Response Format

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"
}

Code Examples

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));