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():
ocrInput.LoadPdf("large.pdf");
ocrInput.LoadPdf("large.pdf");
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.OutOfMemoryExceptionor 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());
Imports IronOcr
Imports IronPdf
Imports System.Text
Dim ocr As New IronTesseract()
Dim pdfPath As String = "large.pdf"
Using pdf = PdfDocument.FromFile(pdfPath)
Dim pageCount As Integer = pdf.PageCount
Dim textBuilder As New StringBuilder()
For i As Integer = 0 To pageCount - 1
Using input As New OcrInput()
input.LoadPdfPage(pdfPath, i, 80)
Dim result = ocr.Read(input)
textBuilder.Append(result.Text)
textBuilder.Append(" ") ' Add space between pages
End Using
Next
Console.WriteLine(textBuilder.ToString().Trim())
End Using
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);
Imports IronTesseract
Dim pdf = PdfDocument.FromFile(pdfPath)
Dim stream = pdf.CompressPdfToStream(CompressStructTree:=True)
Dim ocrTesseract = New IronTesseract()
Using ocrInput As New OcrInput()
ocrInput.LoadPdfPage(stream, 1)
End Using
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);
Imports System
Dim pdf = PdfDocument.FromFile("D:\hugePdf.pdf")
Dim stream = pdf.CompressPdfToStream(JpegQuality:=75, CompressStructTree:=True)
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
usingstatements 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.

