/* :path=/static-assets/ocr/content-code-examples/how-to/input-pdfs-read-pdf.cs */using IronOcr;// Instantiate IronTesseractIronTesseract ocrTesseract = new IronTesseract();// Add PDFusing var pdfInput = new OcrPdfInput("Potter.pdf");// Perform OCROcrResult ocrResult = ocrTesseract.Read(pdfInput);// Access the extracted textstring extractedText = ocrResult.Text;System.Console.WriteLine(extractedText);
/* :path=/static-assets/ocr/content-code-examples/how-to/input-pdfs-read-pdf.cs */
using IronOcr;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add PDF
using var pdfInput = new OcrPdfInput("Potter.pdf");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(pdfInput);
// Access the extracted text
string extractedText = ocrResult.Text;
System.Console.WriteLine(extractedText);
ImportsIronOcr' Instantiate IronTesseractDim ocrTesseract As New IronTesseract()' Add PDFUsing pdfInput As New OcrPdfInput("Potter.pdf") ' Perform OCR Dim ocrResult AsOcrResult = ocrTesseract.Read(pdfInput) ' Access the extracted text Dim extractedText AsString = ocrResult.TextSystem.Console.WriteLine(extractedText)EndUsing
Imports IronOcr
' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()
' Add PDF
Using pdfInput As New OcrPdfInput("Potter.pdf")
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(pdfInput)
' Access the extracted text
Dim extractedText As String = ocrResult.Text
System.Console.WriteLine(extractedText)
End Using
// For text-only PDFs (faster processing)var textOnlyPdf = new OcrPdfInput("document.pdf", PdfContents.Text);// For image-only PDFs (scanned documents)var imageOnlyPdf = new OcrPdfInput("scanned.pdf", PdfContents.Images);// For mixed content (default)var mixedPdf = new OcrPdfInput("mixed.pdf", PdfContents.TextAndImages);
// For text-only PDFs (faster processing)
var textOnlyPdf = new OcrPdfInput("document.pdf", PdfContents.Text);
// For image-only PDFs (scanned documents)
var imageOnlyPdf = new OcrPdfInput("scanned.pdf", PdfContents.Images);
// For mixed content (default)
var mixedPdf = new OcrPdfInput("mixed.pdf", PdfContents.TextAndImages);
' For text-only PDFs (faster processing)Dim textOnlyPdf = New OcrPdfInput("document.pdf", PdfContents.Text)' For image-only PDFs (scanned documents)Dim imageOnlyPdf = New OcrPdfInput("scanned.pdf", PdfContents.Images)' For mixed content (default)Dim mixedPdf = New OcrPdfInput("mixed.pdf", PdfContents.TextAndImages)
' For text-only PDFs (faster processing)
Dim textOnlyPdf = New OcrPdfInput("document.pdf", PdfContents.Text)
' For image-only PDFs (scanned documents)
Dim imageOnlyPdf = New OcrPdfInput("scanned.pdf", PdfContents.Images)
' For mixed content (default)
Dim mixedPdf = New OcrPdfInput("mixed.pdf", PdfContents.TextAndImages)
using IronOcr;using System.Collections.Generic;// Instantiate IronTesseractIronTesseract ocrTesseract = new IronTesseract();// Create page indices listList<int> pageIndices = new List<int>() { 0, 2 };// Add PDFusing var pdfInput = new OcrPdfInput("Potter.pdf", PageIndices: pageIndices);// Perform OCROcrResult ocrResult = ocrTesseract.Read(pdfInput);
using IronOcr;
using System.Collections.Generic;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Create page indices list
List<int> pageIndices = new List<int>() { 0, 2 };
// Add PDF
using var pdfInput = new OcrPdfInput("Potter.pdf", PageIndices: pageIndices);
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(pdfInput);
ImportsIronOcrImportsSystem.Collections.Generic' Instantiate IronTesseractPrivate ocrTesseract As New IronTesseract()' Create page indices listPrivate pageIndices As New List(OfInteger)() From {0, 2}' Add PDFPrivate pdfInput = New OcrPdfInput("Potter.pdf", PageIndices:= pageIndices)' Perform OCRPrivate ocrResult AsOcrResult = ocrTesseract.Read(pdfInput)
Imports IronOcr
Imports System.Collections.Generic
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Create page indices list
Private pageIndices As New List(Of Integer)() From {0, 2}
' Add PDF
Private pdfInput = New OcrPdfInput("Potter.pdf", PageIndices:= pageIndices)
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(pdfInput)
// Read pages 1, 3, 5, and 10 (using zero-based indices)List<int> pageIndices = new List<int>() { 0, 2, 4, 9 };// Or use LINQ for range-based selectionvar evenPages = Enumerable.Range(0, 10).Where(x => x % 2 == 0).ToList();
// Read pages 1, 3, 5, and 10 (using zero-based indices)
List<int> pageIndices = new List<int>() { 0, 2, 4, 9 };
// Or use LINQ for range-based selection
var evenPages = Enumerable.Range(0, 10).Where(x => x % 2 == 0).ToList();
ImportsSystem.Collections.GenericImportsSystem.Linq' Read pages 1, 3, 5, and 10 (using zero-based indices)Dim pageIndices As New List(OfInteger)() From {0, 2, 4, 9}' Or use LINQ for range-based selectionDim evenPages = Enumerable.Range(0, 10).Where(Function(x) x Mod2 = 0).ToList()
Imports System.Collections.Generic
Imports System.Linq
' Read pages 1, 3, 5, and 10 (using zero-based indices)
Dim pageIndices As New List(Of Integer)() From {0, 2, 4, 9}
' Or use LINQ for range-based selection
Dim evenPages = Enumerable.Range(0, 10).Where(Function(x) x Mod 2 = 0).ToList()
using IronOcr;using IronSoftware.Drawing;using System;// Instantiate IronTesseractIronTesseract ocrTesseract = new IronTesseract();// Specify crop regionsRectangle[] scanRegions = { new Rectangle(550, 100, 600, 300) };// Add PDFusing (var pdfInput = new OcrPdfInput("Potter.pdf", ContentAreas: scanRegions)){ // Perform OCR OcrResult ocrResult = ocrTesseract.Read(pdfInput); // Output the result to consoleConsole.WriteLine(ocrResult.Text);}
using IronOcr;
using IronSoftware.Drawing;
using System;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Specify crop regions
Rectangle[] scanRegions = { new Rectangle(550, 100, 600, 300) };
// Add PDF
using (var pdfInput = new OcrPdfInput("Potter.pdf", ContentAreas: scanRegions))
{
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(pdfInput);
// Output the result to console
Console.WriteLine(ocrResult.Text);
}
ImportsIronOcrImportsIronSoftware.DrawingImportsSystem' Instantiate IronTesseractPrivate ocrTesseract As New IronTesseract()' Specify crop regionsPrivate scanRegions() AsRectangle = { New Rectangle(550, 100, 600, 300) }' Add PDFUsing pdfInput = New OcrPdfInput("Potter.pdf", ContentAreas:= scanRegions) ' Perform OCR Dim ocrResult AsOcrResult = ocrTesseract.Read(pdfInput) ' Output the result to consoleConsole.WriteLine(ocrResult.Text)EndUsing
Imports IronOcr
Imports IronSoftware.Drawing
Imports System
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Specify crop regions
Private scanRegions() As Rectangle = { New Rectangle(550, 100, 600, 300) }
' Add PDF
Using pdfInput = New OcrPdfInput("Potter.pdf", ContentAreas:= scanRegions)
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(pdfInput)
' Output the result to console
Console.WriteLine(ocrResult.Text)
End Using
Rectangle[] scanRegions = { new Rectangle(50, 50, 200, 100), // Header region new Rectangle(50, 200, 500, 300), // Main content region new Rectangle(50, 550, 200, 50) // Footer region};
Rectangle[] scanRegions = {
new Rectangle(50, 50, 200, 100), // Header region
new Rectangle(50, 200, 500, 300), // Main content region
new Rectangle(50, 550, 200, 50) // Footer region
};
ImportsSystem.DrawingDim scanRegions AsRectangle() = { New Rectangle(50, 50, 200, 100), ' Header region New Rectangle(50, 200, 500, 300), ' Main content region New Rectangle(50, 550, 200, 50) ' Footer region}
Imports System.Drawing
Dim scanRegions As Rectangle() = {
New Rectangle(50, 50, 200, 100), ' Header region
New Rectangle(50, 200, 500, 300), ' Main content region
New Rectangle(50, 550, 200, 50) ' Footer region
}
您可以使用IronOCR僅需一行程式碼就能從PDF文件中提取文字。只需建立一個IronTesseract實例,並使用OcrPdfInput的Read方法:`using var result = new IronOcr.IronTesseract().Read(new IronOcr.OcrPdfInput("document.pdf", PdfContents.TextAndImages));`。IronOCR可處理掃描PDF(基於圖像)和可搜尋PDF(基於文字)。
What are the advantages of using region-specific OCR over full-page OCR?
Region-specific OCR improves performance by focusing on relevant areas, enhancing accuracy and reducing noise from irrelevant content. It is especially useful for forms and structured documents.
What advanced OCR features does IronOCR offer for PDFs?
IronOCR provides features such as creating searchable PDFs, multithreading for faster processing, image preprocessing, and support for multiple languages, catering to complex document processing needs.
How do you handle invalid page numbers in IronOCR?
If invalid page numbers are specified in IronOCR, it will throw an exception. It is recommended to implement error handling or verify page counts prior to processing.
Can IronOCR process non-consecutive pages from a PDF?
Yes, IronOCR can handle non-consecutive pages by specifying the desired page indices in a list. This allows for selective processing of pages in any order.