Save Searchable PDFs in C# with IronOCR
IronOCR 使 C# 開發人員能夠使用 OCR 技術將掃描的文件和影像轉換為可搜尋的 PDF,僅需幾行程式碼即可支援輸出為檔案、位元組或串流。
可搜尋的 PDF,通常被稱為 OCR(光學字元辨識)PDF,是一種包含掃描影像和機器可讀文字的 PDF 文件。 這些 PDF 文件是透過對掃描的紙質文件或圖像執行 OCR 功能創建的,它可以識別圖像中的文字,並將其轉換為可選擇和可搜尋的文字。
SaveAsSearchablePdf 亦可應用於 ReadScreenShot 及 ReadDocumentAdvanced 的結果,透過照片及進階文件 OCR 工作流程,實現可搜尋 PDF 的建立。 此功能在將紙本檔案數位化,或使舊版 PDF 具備搜尋功能以提升文件管理效率時,尤為實用。
快速入門:一行匯出可搜尋 PDF
設定 RenderSearchablePdf = true,在您的輸入資料上執行 Read(...),並呼叫 SaveAsSearchablePdf(...)。 只需這些步驟,即可使用 IronOCR 生成一份可完整搜尋的 PDF 檔案。
最小工作流程(5 個步驟)
- 下載一個 C# 庫,用於將結果儲存為可搜尋的 PDF 檔案。
- 準備圖像和 PDF 文件以進行 OCR
- 將 RenderSearchablePdf 屬性設定為
true - 使用
SaveAsSearchablePdf方法輸出可搜尋的 PDF 文件 - 將可搜尋的 PDF 匯出為位元組和流
如何將 OCR 結果匯出為可搜尋的 PDF?
若要使用 IronOCR 將結果匯出為可搜尋的 PDF,請將 Configuration.RenderSearchablePdf 屬性設定為 true,從 Read 方法取得 OCR 結果物件,並呼叫 SaveAsSearchablePdf 並傳入輸出檔案路徑。
輸入
《哈利波特》小說中的一頁,已掃描為 TIFF 檔案並透過 OcrImageInput 載入。 該頁面包含密集的印刷文字,是測試可搜尋 PDF 文字層的理想素材。
potter.tiff:掃描的小說頁面,用作 OCR 輸入以產生帶有隱形文字層的可搜尋 PDF。
:path=/static-assets/ocr/content-code-examples/how-to/searchable-pdf-searchable-pdf.cs
using IronOcr;
// Create the OCR engine: defaults to English with balanced speed and accuracy
IronTesseract ocrTesseract = new IronTesseract();
// Required: without this flag the text overlay layer is not built, and SaveAsSearchablePdf produces a plain image PDF
ocrTesseract.Configuration.RenderSearchablePdf = true;
// Wrap the TIFF in OcrImageInput: handles DPI detection and page layout automatically
using var imageInput = new OcrImageInput("Potter.tiff");
// Run OCR; returns a result containing the recognized text and spatial layout data
OcrResult ocrResult = ocrTesseract.Read(imageInput);
// Write the output: the original scanned image is preserved with an invisible text layer on top
ocrResult.SaveAsSearchablePdf("searchablePdf.pdf");
Imports IronOcr
' Create the OCR engine: defaults to English with balanced speed and accuracy
Dim ocrTesseract As New IronTesseract()
' Required: without this flag the text overlay layer is not built, and SaveAsSearchablePdf produces a plain image PDF
ocrTesseract.Configuration.RenderSearchablePdf = True
' Wrap the TIFF in OcrImageInput: handles DPI detection and page layout automatically
Using imageInput As New OcrImageInput("Potter.tiff")
' Run OCR; returns a result containing the recognized text and spatial layout data
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Write the output: the original scanned image is preserved with an invisible text layer on top
ocrResult.SaveAsSearchablePdf("searchablePdf.pdf")
End Using
輸出
searchablePdf.pdf:可搜尋 PDF 輸出檔。請選取或搜尋任何單字,以驗證 OCR 文字層。
生成的 PDF 檔案會嵌入原始掃描頁面影像,並在每個識別出的單字上方疊加一層隱形文字層。 請在檢視器中選取或搜尋任何WORD,以確認該文字層是否存在。
IronOCR 採用特定字型進行疊加顯示,因此渲染後的文字大小可能與原始內容略有差異。
在處理多頁 TIFF 檔案或複雜文件時,IronOCR 會自動處理所有頁面並將其納入輸出結果中。 該函式庫可自動處理頁面排序和文字覆蓋定位,確保文字到圖像的映射準確無誤。
如何從照片或進階文件掃描建立可搜尋的 PDF?
使用 ReadScreenShot 或 ReadDocumentAdvanced 時,亦可輸出可搜尋的 PDF 檔案。 這些方法各自返回的結果類型皆支援 SaveAsSearchablePdf。
呼叫這些方法時,可選擇性地傳入 ModelType。 預設選項為 Normal,而 Enhanced 雖犧牲部分速度,但能提供更高的精準度。
輸入
一張展示繪有文字的牆面壁畫照片,透過 LoadImage 載入。 此情境包含嵌入真實世界環境中的多個 WORD,因此是針對 ReadPhoto 搭配 Enhanced 模型的實用測試。
photo.png:透過 ReadPhoto 搭配 Enhanced 模型載入的壁畫照片,用以生成可搜尋的 PDF 檔案。
using IronOcr;
var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("photo.png");
// ReadPhoto with Enhanced model
OcrPhotoResult photoResult = ocr.ReadPhoto(input, ModelType.Enhanced);
Console.WriteLine(photoResult.Text);
// Save as searchable PDF
byte[] pdfBytes = photoResult.SaveAsSearchablePdf();
File.WriteAllBytes("searchable-photo.pdf", pdfBytes);
using IronOcr;
var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("photo.png");
// ReadPhoto with Enhanced model
OcrPhotoResult photoResult = ocr.ReadPhoto(input, ModelType.Enhanced);
Console.WriteLine(photoResult.Text);
// Save as searchable PDF
byte[] pdfBytes = photoResult.SaveAsSearchablePdf();
File.WriteAllBytes("searchable-photo.pdf", pdfBytes);
Imports IronOcr
Dim ocr As New IronTesseract()
Using input As New OcrInput()
input.LoadImage("photo.png")
' ReadPhoto with Enhanced model
Dim photoResult As OcrPhotoResult = ocr.ReadPhoto(input, ModelType.Enhanced)
Console.WriteLine(photoResult.Text)
' Save as searchable PDF
Dim pdfBytes As Byte() = photoResult.SaveAsSearchablePdf()
File.WriteAllBytes("searchable-photo.pdf", pdfBytes)
End Using
輸出
searchable-photo.pdf:由 ReadPhoto 產出的可搜尋 PDF。其文字層可在任何 PDF 檢視器中支援全文搜尋。
生成的可搜尋 PDF 會在識別出的文字上方疊加一層隱形文字層。 在 PDF 檢視器中搜尋"Milk"會找到 3 個匹配結果,這些結果直接從原始照片中的繪製文字中提取。
此方法同樣適用於 ReadDocumentAdvanced,其會返回 OcrDocAdvancedResult:
輸入
透過 LoadImage 上傳的掃描發票。 它包含結構化欄位(供應商名稱、明細項目和總計),這些欄位會與 ReadDocumentAdvanced 模型進行整合,並被 Enhanced 模型識別並嵌入為可搜尋的文字層。
invoice.png:掃描後的發票已載入 OcrInput,並透過增強型模型傳遞至 ReadDocumentAdvanced。
using IronOcr;
var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("invoice.png");
// ReadDocumentAdvanced with Enhanced model
OcrDocAdvancedResult docResult = ocr.ReadDocumentAdvanced(input, ModelType.Enhanced);
byte[] docPdfBytes = docResult.SaveAsSearchablePdf();
File.WriteAllBytes("searchable-doc.pdf", docPdfBytes);
using IronOcr;
var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("invoice.png");
// ReadDocumentAdvanced with Enhanced model
OcrDocAdvancedResult docResult = ocr.ReadDocumentAdvanced(input, ModelType.Enhanced);
byte[] docPdfBytes = docResult.SaveAsSearchablePdf();
File.WriteAllBytes("searchable-doc.pdf", docPdfBytes);
Imports IronOcr
Dim ocr As New IronTesseract()
Using input As New OcrInput()
input.LoadImage("invoice.png")
' ReadDocumentAdvanced with Enhanced model
Dim docResult As OcrDocAdvancedResult = ocr.ReadDocumentAdvanced(input, ModelType.Enhanced)
Dim docPdfBytes As Byte() = docResult.SaveAsSearchablePdf()
File.WriteAllBytes("searchable-doc.pdf", docPdfBytes)
End Using
輸出
searchable-doc.pdf:由 ReadDocumentAdvanced 產出的可搜尋 PDF。發票欄位可供選取與搜尋。
SaveAsSearchablePdf 不支援 ReadPassport 或 ReadLicensePlate 的結果,並會拋出 ExtensionAdvancedScanException。
處理多頁文件
在處理多頁文件的 PDF OCR 作業時,IronOCR 會依序處理每一頁,並保留原始文件的結構。
輸入
由 OcrPdfInput 載入的 Hartwell Capital Management 11 頁年度報告。 透過 PageIndices 範圍選取第 1–10 頁(索引 0–9),並在單一 Read 呼叫中進行處理。
multi-page-scan.pdf:一份 11 頁的 Hartwell Capital Management 年報,用作多頁可搜尋 PDF 轉換的輸入檔案。
:path=/static-assets/ocr/content-code-examples/how-to/searchable-pdf-multi-page.cs
using IronOcr;
// Create the OCR engine. RenderSearchablePdf is false by default; no need to set it when using OcrPdfInput directly
var ocrTesseract = new IronTesseract();
// Load pages 1–10 (indices 0–9) only; PageIndices avoids loading and OCR-ing the full document unnecessarily
using var pdfInput = new OcrPdfInput("multi-page-scan.pdf", PageIndices: Enumerable.Range(0, 10));
// Run OCR across all selected pages in order
OcrResult result = ocrTesseract.Read(pdfInput);
// Write the searchable PDF; true = apply the input's image filters to the embedded page images in the output
result.SaveAsSearchablePdf("searchable-multi-page.pdf", true);
Imports IronOcr
' Create the OCR engine. RenderSearchablePdf is false by default; no need to set it when using OcrPdfInput directly
Dim ocrTesseract As New IronTesseract()
' Load pages 1–10 (indices 0–9) only; PageIndices avoids loading and OCR-ing the full document unnecessarily
Using pdfInput As New OcrPdfInput("multi-page-scan.pdf", PageIndices:=Enumerable.Range(0, 10))
' Run OCR across all selected pages in order
Dim result As OcrResult = ocrTesseract.Read(pdfInput)
' Write the searchable PDF; true = apply the input's image filters to the embedded page images in the output
result.SaveAsSearchablePdf("searchable-multi-page.pdf", True)
End Using
輸出
searchable-multi-page.pdf:10 頁的可搜尋 PDF 輸出檔。每頁皆含一個隱形文字層,以支援全文檢索。
生成的 PDF 文件共 10 頁(源自原始報告的第 1 至 10 頁),每頁均含一個隱形文字圖層,使提取的內容可在任何 PDF 檢視器中選取及搜尋。
在建立可搜尋的 PDF 時,如何套用篩選器?
SaveAsSearchablePdf 第二個參數接受一個布林值,用以控制是否對嵌入式輸出套用影像濾鏡。 使用影像最佳化篩選器可大幅提升 OCR 準確度,尤其是在處理低品質掃描時。
以下範例應用灰階濾鏡,並將 true 作為第二個參數傳入,以將濾鏡處理後的圖片嵌入可搜尋的 PDF 輸出檔中。
:path=/static-assets/ocr/content-code-examples/how-to/image-quality-correction-searchable-pdf.cs
using IronOcr;
// Create OCR engine: filters are applied at the OcrInput level, so no configuration changes are needed here
var ocr = new IronTesseract();
var ocrInput = new OcrInput();
// Load the scanned PDF as the OCR source
ocrInput.LoadPdf("invoice.pdf");
// Convert to grayscale: removes color noise that can reduce OCR accuracy on color-printed documents
ocrInput.ToGrayScale();
// Run OCR on the preprocessed input
OcrResult result = ocr.Read(ocrInput);
// Write the searchable PDF; true = embed the grayscale-filtered image rather than the original color scan
result.SaveAsSearchablePdf("outputGrayscale.pdf", true);
Imports IronOcr
' Create OCR engine: filters are applied at the OcrInput level, so no configuration changes are needed here
Dim ocr As New IronTesseract()
Dim ocrInput As New OcrInput()
' Load the scanned PDF as the OCR source
ocrInput.LoadPdf("invoice.pdf")
' Convert to grayscale: removes color noise that can reduce OCR accuracy on color-printed documents
ocrInput.ToGrayScale()
' Run OCR on the preprocessed input
Dim result As OcrResult = ocr.Read(ocrInput)
' Write the searchable PDF; True = embed the grayscale-filtered image rather than the original color scan
result.SaveAsSearchablePdf("outputGrayscale.pdf", True)
為達到最佳效果,請考慮使用 過濾精靈 自動決定特定文件類型的最佳過濾器組合。 此工具會分析您的輸入,並建議適當的預處理步驟。
如何修復可搜尋PDF中的錯誤字元?
如果 PDF 中的文字在視覺上顯示正確,但在搜尋或複製時顯示損壞的字符,則該問題是由可搜尋文字層中使用的預設字體引起的。 預設情況下,SaveAsSearchablePdf 使用 Times New Roman 字型,該字型無法完全支援所有 Unicode 字元。 這會影響包含重音字元或非 ASCII 字元的語言。
要解決這個問題,請提供與 Unicode 相容的字型檔案作為第三個參數:
result.SaveAsSearchablePdf("output.pdf", false, "Fonts/LiberationSerif-Regular.ttf");
result.SaveAsSearchablePdf("output.pdf", false, "Fonts/LiberationSerif-Regular.ttf");
result.SaveAsSearchablePdf("output.pdf", False, "Fonts/LiberationSerif-Regular.ttf")
您也可以將自訂字型名稱指定為第四個參數:
result.SaveAsSearchablePdf("output.pdf", false, "Fonts/LiberationSerif-Regular.ttf", "MyFont");
result.SaveAsSearchablePdf("output.pdf", false, "Fonts/LiberationSerif-Regular.ttf", "MyFont");
result.SaveAsSearchablePdf("output.pdf", False, "Fonts/LiberationSerif-Regular.ttf", "MyFont")
此修正適用於所有結果類型,包括 OcrPhotoResult 及 OcrDocAdvancedResult,因此無論結果由哪種讀取方法產生,此修正皆能生效。
對於最初使用 Times New Roman 排版的文檔,建議使用 Liberation Serif,因為它在度量上是相容的,可以保留原始的間距和佈局。 對於通用多語言用途,Noto Sans 或 DejaVu Sans 都是不錯的選擇。
若無法寫入檔案路徑,IronOCR 亦支援將可搜尋的 PDF 以位元組陣列或串流形式回傳。
如何將可搜尋的 PDF 匯出為位元組或資料流?
可搜尋 PDF 的輸出內容亦可透過 SaveAsSearchablePdfBytes 和 SaveAsSearchablePdfStream 方法,分別以位元組或串流形式進行處理。 以下程式碼範例展示了如何使用這些方法。
:path=/static-assets/ocr/content-code-examples/how-to/searchable-pdf-searchable-pdf-byte-stream.cs
// Return as a byte array: suited for storing in a database or sending in an HTTP response body
byte[] pdfByte = ocrResult.SaveAsSearchablePdfBytes();
// Return as a stream: suited for uploading to cloud storage or piping to another I/O operation without buffering the full file
Stream pdfStream = ocrResult.SaveAsSearchablePdfStream();
' Return as a byte array: suited for storing in a database or sending in an HTTP response body
Dim pdfByte As Byte() = ocrResult.SaveAsSearchablePdfBytes()
' Return as a stream: suited for uploading to cloud storage or piping to another I/O operation without buffering the full file
Dim pdfStream As Stream = ocrResult.SaveAsSearchablePdfStream()
在與檔案系統存取可能受限的雲端儲存服務、資料庫或網路應用程式整合時,這些輸出選項尤其有用。 以下範例展示實際應用:
using IronOcr;
using System.IO;
public class SearchablePdfExporter
{
public async Task ProcessAndUploadPdf(string inputPath)
{
var ocr = new IronTesseract
{
Configuration = { RenderSearchablePdf = true }
};
// Process the input
using var input = new OcrImageInput(inputPath);
var result = ocr.Read(input);
// Option 1: Save to database as byte array
byte[] pdfBytes = result.SaveAsSearchablePdfBytes();
// Store pdfBytes in database BLOB field
// Option 2: Upload to cloud storage using stream
using (Stream pdfStream = result.SaveAsSearchablePdfStream())
{
// Upload stream to Azure Blob Storage, AWS S3, etc.
await UploadToCloudStorage(pdfStream, "searchable-output.pdf");
}
// Option 3: Return as web response
// return File(pdfBytes, "application/pdf", "searchable.pdf");
}
private async Task UploadToCloudStorage(Stream stream, string fileName)
{
// Cloud upload implementation
}
}
using IronOcr;
using System.IO;
public class SearchablePdfExporter
{
public async Task ProcessAndUploadPdf(string inputPath)
{
var ocr = new IronTesseract
{
Configuration = { RenderSearchablePdf = true }
};
// Process the input
using var input = new OcrImageInput(inputPath);
var result = ocr.Read(input);
// Option 1: Save to database as byte array
byte[] pdfBytes = result.SaveAsSearchablePdfBytes();
// Store pdfBytes in database BLOB field
// Option 2: Upload to cloud storage using stream
using (Stream pdfStream = result.SaveAsSearchablePdfStream())
{
// Upload stream to Azure Blob Storage, AWS S3, etc.
await UploadToCloudStorage(pdfStream, "searchable-output.pdf");
}
// Option 3: Return as web response
// return File(pdfBytes, "application/pdf", "searchable.pdf");
}
private async Task UploadToCloudStorage(Stream stream, string fileName)
{
// Cloud upload implementation
}
}
Imports IronOcr
Imports System.IO
Imports System.Threading.Tasks
Public Class SearchablePdfExporter
Public Async Function ProcessAndUploadPdf(inputPath As String) As Task
Dim ocr As New IronTesseract With {
.Configuration = New TesseractConfiguration With {
.RenderSearchablePdf = True
}
}
' Process the input
Using input As New OcrImageInput(inputPath)
Dim result = ocr.Read(input)
' Option 1: Save to database as byte array
Dim pdfBytes As Byte() = result.SaveAsSearchablePdfBytes()
' Store pdfBytes in database BLOB field
' Option 2: Upload to cloud storage using stream
Using pdfStream As Stream = result.SaveAsSearchablePdfStream()
' Upload stream to Azure Blob Storage, AWS S3, etc.
Await UploadToCloudStorage(pdfStream, "searchable-output.pdf")
End Using
' Option 3: Return as web response
' Return File(pdfBytes, "application/pdf", "searchable.pdf")
End Using
End Function
Private Async Function UploadToCloudStorage(stream As Stream, fileName As String) As Task
' Cloud upload implementation
End Function
End Class
效能考量
在處理大量文件時,請考慮實施 多執行緒 OCR 作業,以提高吞吐量。 IronOCR 支援並發處理,可讓您同時處理多個文件:
using IronOcr;
using System.Threading.Tasks;
using System.Collections.Concurrent;
public class BatchPdfProcessor
{
private readonly IronTesseract _ocr;
public BatchPdfProcessor()
{
_ocr = new IronTesseract
{
Configuration =
{
RenderSearchablePdf = true,
// Configure for optimal performance
Language = OcrLanguage.English
}
};
}
public async Task ProcessBatchAsync(string[] filePaths)
{
var results = new ConcurrentBag<(string source, string output)>();
await Parallel.ForEachAsync(filePaths, async (filePath, ct) =>
{
using var input = new OcrImageInput(filePath);
var result = _ocr.Read(input);
string outputPath = Path.ChangeExtension(filePath, ".searchable.pdf");
result.SaveAsSearchablePdf(outputPath);
results.Add((filePath, outputPath));
});
Console.WriteLine($"Processed {results.Count} files");
}
}
using IronOcr;
using System.Threading.Tasks;
using System.Collections.Concurrent;
public class BatchPdfProcessor
{
private readonly IronTesseract _ocr;
public BatchPdfProcessor()
{
_ocr = new IronTesseract
{
Configuration =
{
RenderSearchablePdf = true,
// Configure for optimal performance
Language = OcrLanguage.English
}
};
}
public async Task ProcessBatchAsync(string[] filePaths)
{
var results = new ConcurrentBag<(string source, string output)>();
await Parallel.ForEachAsync(filePaths, async (filePath, ct) =>
{
using var input = new OcrImageInput(filePath);
var result = _ocr.Read(input);
string outputPath = Path.ChangeExtension(filePath, ".searchable.pdf");
result.SaveAsSearchablePdf(outputPath);
results.Add((filePath, outputPath));
});
Console.WriteLine($"Processed {results.Count} files");
}
}
Imports IronOcr
Imports System.Threading.Tasks
Imports System.Collections.Concurrent
Public Class BatchPdfProcessor
Private ReadOnly _ocr As IronTesseract
Public Sub New()
_ocr = New IronTesseract With {
.Configuration = New OcrConfiguration With {
.RenderSearchablePdf = True,
' Configure for optimal performance
.Language = OcrLanguage.English
}
}
End Sub
Public Async Function ProcessBatchAsync(filePaths As String()) As Task
Dim results As New ConcurrentBag(Of (source As String, output As String))()
Await Task.Run(Sub()
Parallel.ForEach(filePaths, Sub(filePath)
Using input As New OcrImageInput(filePath)
Dim result = _ocr.Read(input)
Dim outputPath As String = Path.ChangeExtension(filePath, ".searchable.pdf")
result.SaveAsSearchablePdf(outputPath)
results.Add((filePath, outputPath))
End Using
End Sub)
End Sub)
Console.WriteLine($"Processed {results.Count} files")
End Function
End Class
進階組態選項
對於更進階的情境,您可以利用 詳細的 Tesseract 配置,針對特定的文件類型或語言微調 OCR 引擎:
var advancedOcr = new IronTesseract
{
Configuration =
{
RenderSearchablePdf = true,
TesseractVariables = new Dictionary<string, object>
{
{ "preserve_interword_spaces", 1 },
{ "tessedit_char_whitelist", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" }
},
PageSegmentationMode = TesseractPageSegmentationMode.SingleColumn
},
Language = OcrLanguage.EnglishBest
};
var advancedOcr = new IronTesseract
{
Configuration =
{
RenderSearchablePdf = true,
TesseractVariables = new Dictionary<string, object>
{
{ "preserve_interword_spaces", 1 },
{ "tessedit_char_whitelist", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" }
},
PageSegmentationMode = TesseractPageSegmentationMode.SingleColumn
},
Language = OcrLanguage.EnglishBest
};
Imports IronOcr
Dim advancedOcr As New IronTesseract With {
.Configuration = New TesseractConfiguration With {
.RenderSearchablePdf = True,
.TesseractVariables = New Dictionary(Of String, Object) From {
{"preserve_interword_spaces", 1},
{"tessedit_char_whitelist", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"}
},
.PageSegmentationMode = TesseractPageSegmentationMode.SingleColumn
},
.Language = OcrLanguage.EnglishBest
}
以下設定選項同樣適用於所有三種輸出方式:SaveAsSearchablePdfBytes 以及 SaveAsSearchablePdfStream。 下方的摘要彙整了所有可搜尋的 PDF 處理方法及其對應的輸出格式。
摘要
使用 IronOCR 創建可搜尋的 PDF 既直接又靈活。 無論您需要處理單張圖片、多頁文件、透過 ReadPhoto 處理的照片,或是透過 ReadDocumentAdvanced 處理的高階文件掃描檔,此函式庫皆提供強大的方法,可生成多種格式的可搜尋 PDF 檔案。 請使用 ModelType 參數,在標準與進階機器學習模型之間進行選擇,以確保翻譯準確性。 以檔案、位元組或串流形式匯出的能力,使其可適用於任何應用程式架構,從桌上型電腦應用程式到雲端服務。
若需處理更複雜的 OCR 情境,請參閱詳盡的程式碼範例,或查閱 API 文件以了解詳細的方法簽名與選項。
常見問題解答
如何使用 C# 從掃描的影像建立可搜尋的 PDF?
IronOCR 可讓您輕鬆地從掃描影像建立可搜尋的 PDF。只需在設定中將 RenderSearchablePdf 設為 true,在輸入影像上使用 Read() 方法,然後以所需的輸出路徑呼叫 SaveAsSearchablePdf()。IronOcr 將會對影像執行 OCR,並產生 PDF,在原始影像上覆蓋可選擇、可搜尋的文字。
哪些檔案格式可以轉換成可搜尋的 PDF?
IronOCR 可以將 JPG、PNG、TIFF 等多種圖像格式以及現有的 PDF 文件轉換為可搜尋的 PDF。該函式庫支援單頁影像和 TIFF 檔案等多頁文件,可自動處理所有頁面,並在輸出的可搜尋 PDF 中維持適當的頁面排序。
我可以將可搜尋的 PDF 匯出為位元組陣列或串流,而不是檔案嗎?
是的,IronOCR 支援导出多种格式的可搜索 PDF。除了使用 SaveAsSearchablePdf() 直接儲存至檔案外,您也可以將 OCR 結果匯出為位元組陣列或串流,讓您輕鬆與網路應用程式、雲端儲存或資料庫系統整合,而無需建立臨時檔案。
建立可搜尋的 PDF 至少需要多少程式碼?
使用 IronOCR 建立可搜尋的 PDF 只需一行程式碼即可完成: new IronOcr.IronTesseract { Configuration = { RenderSearchablePdf = true }.}.Read(new IronOcr.OcrImageInput("file.jpg")).SaveAsSearchablePdf("searchable.pdf").這展示了 IronOCR 精簡的 API 設計。
在可搜尋 PDF 中,隱藏文字層是如何運作的?
IronOCR 會自動將辨識出的文字定位為 PDF 原始影像上方的隱形圖層。此機制確保文字與影像的對應關係精準無誤,讓使用者能在維持原始文件視覺外觀的同時,選取並搜尋文字內容。該程式庫透過專用字型與定位演算法來實現此功能。
我可以將照片或螢幕截圖轉為可搜尋的 PDF 檔案嗎?
是的,ReadPhoto、ReadScreenShot 和 ReadDocumentAdvanced 的結果皆支援 SaveAsSearchablePdf 功能。每個方法返回的結果類型均支援可搜尋 PDF 匯出,讓您能輕鬆將實際照片、螢幕截圖或複雜的文件掃描檔轉換為可搜尋的 PDF 檔案。
ModelType 參數的作用是什麼?
ModelType 參數用於控制 OCR 所使用的預訓練機器學習模型。預設值為 Normal,會將圖片縮放至 960 像素以獲得快速結果。Enhanced 則支援最高 2560 像素的圖片,能保留更細緻的細節,並針對高解析度輸入提升準確度。
為什麼我在可搜尋 PDF 中複製或搜尋的字元會顯示為損壞?
發生這種情況是因為可搜尋文字層所使用的預設字型(Times New Roman)並未完全支援所有 Unicode 字元。要解決此問題,請將相容 Unicode 的字型檔案作為 SaveAsSearchablePdf 的第三個參數傳入。若您的文件原始排版使用的是 Times New Roman,且發現與其他字型存在間距不一致的情況,請嘗試使用 Liberation Serif,因為它具有相同的字形度量,並能保留原始版面配置。

