# Processing Large Multi-Page PDFs with IronOCR
Large multi-page PDFs can push IronOCR into performance or memory trouble if the document is loaded all at once. The fix is a page-by-page workflow that keeps memory flat and avoids `OutOfMemoryException`.
## The Problem with `OcrInput.LoadPdf()`
A common first attempt is to hand the whole file to `LoadPdf()`:
```csharp
ocrInput.LoadPdf("large.pdf");
```
This loads every page at once, which is what triggers the trouble:
- Memory spikes sharply as the entire document enters memory.
- IronOCR's internal imaging system processes all pages simultaneously.
- DPI defaults to `200`, which drives memory use higher still when left unset.
- The result can be `System.OutOfMemoryException` or resource deadlocks.
## Solution
Process one page at a time with `LoadPdfPage()` instead of loading the whole document.
### 1. Get the PDF Page Count
Read the page count first with `IronPdf.PdfDocument` (or any open-source PDF library) so you know how many iterations you need.
### 2. Loop Through Pages
Drive a `for` loop over the page indexes and handle each page on its own.
### 3. Load an Individual Page
Inside the loop, call `OcrInput.LoadPdfPage("file.pdf", pageIndex, dpi)` to bring in a single page. When visual quality allows, drop the DPI as low as `80` to conserve memory.
### 4. Extract Text Per Page
Pass the input to `IronTesseract.Read()` to OCR that one page.
### 5. Build the Full Text
Concatenate each page's text into a `StringBuilder` as you go.
```csharp
var ocr = new IronTesseract();
var pdfPath = "large.pdf";
using var pdf = IronPdf.PdfDocument.FromFile(pdfPath);
var pageCount = pdf.PageCount;
var textBuilder = new StringBuilder();
for (int i = 0; i < pageCount; i++)
{
using var input = new OcrInput();
input.LoadPdfPage(pdfPath, i, 80);
var result = ocr.Read(input);
textBuilder.Append(result.Text);
textBuilder.Append(' '); // Add space between pages
}
Console.WriteLine(textBuilder.ToString().Trim());
```
The `using` on each `OcrInput` releases the page's imaging data before the next iteration, so memory does not accumulate across the loop.
## Compress the PDF First for Image-Heavy Files
If the page-by-page approach still isn't fast or stable enough, compress the PDF with IronPDF's Compress API before handing it to IronOCR. This cuts the amount of image data IronOCR has to process, which pays off most on scanned or image-heavy documents.
Compress the file to a stream, then load that stream straight into `OcrInput`:
```csharp
var pdf = PdfDocument.FromFile(pdfPath);
var stream = pdf.CompressPdfToStream(CompressStructTree: true);
var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();
ocrInput.LoadPdfPage(stream, 1);
```
For PDFs that carry images, lower the `JpegQuality` during compression to shrink the data further:
```csharp
var pdf = PdfDocument.FromFile(@"D:\hugePdf.pdf");
var stream = pdf.CompressPdfToStream(JpegQuality: 75, CompressStructTree: true);
```
[[w:(When reusing the same MemoryStream across loop iterations, reset its position to 0 before each read. A stream is consumed once read, so subsequent reads fail if the position isn't reset.)]]
## Debug Tips
- **Lower the DPI (80 to 100):** when visual quality allows, this cuts memory usage noticeably.
- **Avoid reading the entire PDF at once** unless the file is very small.
- **Dispose objects properly** with `using` statements to release memory early.
- Run OCR in parallel only if the machine has enough memory and CPU headroom; be cautious with large PDFs.
Combining IronOCR with IronPDF (or another PDF utility) lets you process large documents page by page without crashing the application, keeping the workflow stable and scalable in production.
Large multi-page PDFs can push IronOCR into performance or memory trouble if the document is loaded all at once. The fix is a page-by-page workflow that keeps memory flat and avoids OutOfMemoryException.
The Problem with OcrInput.LoadPdf()
A common first attempt is to hand the whole file to LoadPdf():
ocrInput.LoadPdf("large.pdf");
ocrInput.LoadPdf("large.pdf");
C#
This loads every page at once, which is what triggers the trouble:
Memory spikes sharply as the entire document enters memory.
IronOCR's internal imaging system processes all pages simultaneously.
DPI defaults to 200, which drives memory use higher still when left unset.
The result can be System.OutOfMemoryException or resource deadlocks.
Solution
Process one page at a time with LoadPdfPage() instead of loading the whole document.
1. Get the PDF Page Count
Read the page count first with IronPdf.PdfDocument (or any open-source PDF library) so you know how many iterations you need.
2. Loop Through Pages
Drive a for loop over the page indexes and handle each page on its own.
3. Load an Individual Page
Inside the loop, call OcrInput.LoadPdfPage("file.pdf", pageIndex, dpi) to bring in a single page. When visual quality allows, drop the DPI as low as 80 to conserve memory.
4. Extract Text Per Page
Pass the input to IronTesseract.Read() to OCR that one page.
5. Build the Full Text
Concatenate each page's text into a StringBuilder as you go.
var ocr = new IronTesseract();var pdfPath = "large.pdf";using var pdf = IronPdf.PdfDocument.FromFile(pdfPath);var pageCount = pdf.PageCount;var textBuilder = new StringBuilder();for (int i = 0; i < pageCount; i++){ using var input = new OcrInput(); input.LoadPdfPage(pdfPath, i, 80); var result = ocr.Read(input); textBuilder.Append(result.Text); textBuilder.Append(' '); // Add space between pages}Console.WriteLine(textBuilder.ToString().Trim());
var ocr = new IronTesseract();
var pdfPath = "large.pdf";
using var pdf = IronPdf.PdfDocument.FromFile(pdfPath);
var pageCount = pdf.PageCount;
var textBuilder = new StringBuilder();
for (int i = 0; i < pageCount; i++)
{
using var input = new OcrInput();
input.LoadPdfPage(pdfPath, i, 80);
var result = ocr.Read(input);
textBuilder.Append(result.Text);
textBuilder.Append(' '); // Add space between pages
}
Console.WriteLine(textBuilder.ToString().Trim());
C#
The using on each OcrInput releases the page's imaging data before the next iteration, so memory does not accumulate across the loop.
Compress the PDF First for Image-Heavy Files
If the page-by-page approach still isn't fast or stable enough, compress the PDF with IronPDF's Compress API before handing it to IronOCR. This cuts the amount of image data IronOCR has to process, which pays off most on scanned or image-heavy documents.
Compress the file to a stream, then load that stream straight into OcrInput:
var pdf = PdfDocument.FromFile(pdfPath);var stream = pdf.CompressPdfToStream(CompressStructTree: true);var ocrTesseract = new IronTesseract();using var ocrInput = new OcrInput();ocrInput.LoadPdfPage(stream, 1);
var pdf = PdfDocument.FromFile(pdfPath);
var stream = pdf.CompressPdfToStream(CompressStructTree: true);
var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();
ocrInput.LoadPdfPage(stream, 1);
C#
For PDFs that carry images, lower the JpegQuality during compression to shrink the data further:
var pdf = PdfDocument.FromFile(@"D:\hugePdf.pdf");var stream = pdf.CompressPdfToStream(JpegQuality: 75, CompressStructTree: true);
var pdf = PdfDocument.FromFile(@"D:\hugePdf.pdf");
var stream = pdf.CompressPdfToStream(JpegQuality: 75, CompressStructTree: true);
C#
Warning: When reusing the same MemoryStream across loop iterations, reset its position to 0 before each read. A stream is consumed once read, so subsequent reads fail if the position isn't reset.
Debug Tips
Lower the DPI (80 to 100): when visual quality allows, this cuts memory usage noticeably.
Avoid reading the entire PDF at once unless the file is very small.
Dispose objects properly with using statements to release memory early.
Run OCR in parallel only if the machine has enough memory and CPU headroom; be cautious with large PDFs.
Combining IronOCR with IronPDF (or another PDF utility) lets you process large documents page by page without crashing the application, keeping the workflow stable and scalable in production.
Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.