In many business workflows, scanned PDF documents must be transformed into searchable or editable text. The Extract Text from Scanned PDFs with Aspose.PDF OCR in C# tutorial demonstrates how to harness the integrated OCR engine in Aspose.PDF for .NET to pull plain text from image‑based pages. By following this guide you’ll get a ready‑to‑run solution that works with any scanned PDF, regardless of its size or language.
Extract Text from Scanned PDFs using Aspose.PDF OCR SDK
Scanned PDFs contain only images, so typical text extraction APIs return nothing. OCR (Optical Character Recognition) converts those images into machine‑readable characters, enabling indexing, data mining, and downstream processing. Automating OCR in C# removes manual copy‑paste steps, improves accuracy, and scales across large document batches.
Aspose.PDF provides the Aspose.Pdf.Ocr namespace, which includes OcrTextRecognitionOptions, OcrTextAbsorber, and language enums. The library is distributed via NuGet. Install it with the following command:
Install-Package Aspose.PDF
For more information, visit the Aspose.PDF product page.
Extract Text from Scanned PDFs with Aspose.PDF OCR in C#
- Load the scanned PDF into an
Aspose.Pdf.Document. - Configure OCR options (language, resolution, page separator).
- Apply an
OcrTextAbsorberto the document’s page collection. - Retrieve the recognized text and save it.
The example demonstrates loading a scanned PDF, configuring OCR, extracting text, and writing the result to a file using C#.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void RecognizeTextWithOcr()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "input.pdf"))
{
// Configure OCR recognition options
var options = new Aspose.Pdf.Ocr.OcrTextRecognitionOptions
{
Language = Aspose.Pdf.Ocr.OcrLanguage.English,
Resolution = 300,
PageSeparator = "\n\n"
};
// Recognize text from all pages
var absorber = new Aspose.Pdf.Ocr.OcrTextAbsorber(options);
document.Pages.Accept(absorber);
// Save recognized text
System.IO.File.WriteAllText(dataDir + "recognized-text.txt", absorber.Text);
}
}
Aspose.PDF Features that Matter for this Task
- Integrated OCR engine – No external dependencies; the OCR engine lives in the
Aspose.Pdf.Ocrnamespace. - Language selection –
OcrLanguageenum lets you specify the dictionary for better recognition. - Resolution control – Higher DPI improves character fidelity, especially for faint scans.
- PageSeparator – Customizable string to separate pages in the output text.
- Thread‑safe page collection – Allows parallel processing of large PDFs.
These capabilities enable a robust, single‑library solution for extracting text from any scanned PDF.
Installation and Setup in C#
- Open your .NET solution in Visual Studio.
- Open the Package Manager Console and run
Install-Package Aspose.PDF. - Add
using Aspose.Pdf;andusing Aspose.Pdf.Ocr;at the top of your class file. - Ensure you have a temporary or full license; without it, the output may contain a watermark.
For licensing details, see the temporary license page.
Step‑by‑Step Implementation in C#
What does this section answer? What are the exact code steps to extract text?
- Initialize the document
var pdfPath = "path/to/scanned.pdf"; var document = new Document(pdfPath); - Create OCR options
var ocrOptions = new OcrTextRecognitionOptions { Language = OcrLanguage.English, Resolution = 300, PageSeparator = "\n--- Page Break ---\n" }; - Run the absorber
var absorber = new OcrTextAbsorber(ocrOptions); document.Pages.Accept(absorber); - Access the extracted text
string extractedText = absorber.Text; - Save or process the text
System.IO.File.WriteAllText("output.txt", extractedText);
The code follows the same flow as the comprehensive example above but breaks the logic into digestible steps.
Get a Free License
Evaluate Aspose.PDF without cost by requesting a temporary license here: https://purchase.aspose.com/temporary-license/.
Free Additional Resources
Conclusion
This guide walked you through extracting text from scanned PDFs using Aspose.PDF OCR in C#. You learned how to install the library, configure OCR options for accuracy, implement a clean extraction pipeline, and apply performance optimizations for large documents. With these techniques, any .NET developer can turn image‑only PDFs into searchable, editable text streams.
FAQs
- How do I extract text from a scanned PDF in C# using Aspose.PDF?
Create a
Document, configureOcrTextRecognitionOptions, run anOcrTextAbsorberon the pages, and read theTextproperty. - Which OCR languages are supported by Aspose.PDF?
The library supports many languages via the
OcrLanguageenum, including English, French, German, Spanish, Chinese, and more. - How can I improve OCR accuracy for low‑resolution scans?
Increase the
Resolution(e.g., 300 DPI), select the correct language, and consider pre‑processing images to boost contrast. - Is it safe to process large PDFs in parallel? Yes, by splitting the document into page ranges and processing each range on a separate thread, while monitoring memory usage.
- What licensing is required for OCR features? OCR is fully available with a valid Aspose.PDF for .NET license; a temporary license can be obtained for evaluation.
- Where can I find more documentation on OCR settings? Detailed OCR configuration guidance is available in the Aspose.PDF OCR settings docs at https://docs.aspose.com/pdf/net/.
