IronOCR 操作指南 可搜尋的 PDF Save 可搜索的 PDFs in C# with IronOCR Save 可搜索的 PDFs in C# with IronOCR Curtis Chau 更新:2026年1月10日 下載 IronOCR NuGet 下載 DLL 下載 Windows 安裝程式 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 This article was translated from English: Does it need improvement? Translated View the article in English IronOCR 使 C# 開發人員能夠使用 OCR 技術將掃描的文件和影像轉換為可搜尋的 PDF,僅需幾行程式碼即可支援輸出為檔案、位元組或串流。 可搜尋的 PDF,通常被稱為 OCR(光學字元辨識)PDF,是一種包含掃描影像和機器可讀文字的 PDF 文件。 這些 PDF 文件是透過對掃描的紙質文件或圖像執行 OCR 功能創建的,它可以識別圖像中的文本,並將其轉換為可選擇和可搜尋的文本。 IronOCR 提供了一種解決方案,可以對文件執行光學字元識別,並將結果匯出為可搜尋的 PDF 文件。 它支援將可搜尋的 PDF 匯出為文件、位元組和流。 這項能力在處理 掃描文件、紙張檔案數位化,或使傳統 PDF 檔案可搜尋以改善文件管理時特別有用。 快速入門:一行匯出可搜尋的 PDF <! -- --> <!--說明:說明程式碼概念的圖表或截圖 --> 設置 RenderSearchablePdf = true,在您的輸入上執行 Read(...) 並調用 SaveAsSearchablePdf(...) - 這就是使用 IronOCR 產生一個完全可搜尋的 PDF 所需要的全部。 立即開始使用 NuGet 建立 PDF 檔案: 使用 NuGet 套件管理器安裝 IronOCR PM > Install-Package IronOcr 複製並運行這段程式碼。 new IronOcr.IronTesseract { Configuration = { RenderSearchablePdf = true } } .Read(new IronOcr.OcrImageInput("file.jpg")).SaveAsSearchablePdf("searchable.pdf"); 部署到您的生產環境進行測試 立即開始在您的專案中使用 IronOCR,免費試用! 免費試用30天 ### 最小工作流程(5 個步驟) 下載一個 C# 庫,用於將結果儲存為可搜尋的 PDF 檔案。 準備圖像和 PDF 文件以進行 OCR 將 RenderSearchablePdf 屬性設定為 true 使用SaveAsSearchablePdf方法輸出可搜尋的 PDF 文件 將可搜尋的 PDF 匯出為位元組和流 如何將 OCR 結果匯出為可搜尋的 PDF? <! -- --> --> <!--說明:顯示程式碼執行輸出或結果的截圖 --> 以下是如何使用 IronOCR 將結果匯出為可搜尋的 PDF 檔案。 您必須先將Configuration.RenderSearchablePdf屬性設為true 。 透過Read方法取得 OCR 結果物件後,使用SaveAsSearchablePdf方法指定輸出檔案路徑。 以下程式碼示範如何使用範例 TIFF 檔案。 :path=/static-assets/ocr/content-code-examples/how-to/searchable-pdf-searchable-pdf.cs using IronOcr; // Instantiate IronTesseract IronTesseract ocrTesseract = new IronTesseract(); // Enable render as searchable PDF ocrTesseract.Configuration.RenderSearchablePdf = true; // Add image using var imageInput = new OcrImageInput("Potter.tiff"); // Perform OCR OcrResult ocrResult = ocrTesseract.Read(imageInput); // Export as searchable PDF ocrResult.SaveAsSearchablePdf("searchablePdf.pdf"); Imports IronOcr ' Instantiate IronTesseract Private ocrTesseract As New IronTesseract() ' Enable render as searchable PDF ocrTesseract.Configuration.RenderSearchablePdf = True ' Add image Dim imageInput = New OcrImageInput("Potter.tiff") ' Perform OCR Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput) ' Export as searchable PDF ocrResult.SaveAsSearchablePdf("searchablePdf.pdf") $vbLabelText $csharpLabel 在處理 多頁 TIFF 檔案或複雜文件時,IronOCR 會自動處理所有頁面,並將其納入可搜尋的 PDF 輸出中。 該函式庫可自動處理頁面排序和文字覆蓋定位,確保文字到圖像的映射準確無誤。 下面顯示的是範例 TIFF 檔案的螢幕截圖以及嵌入的可搜尋 PDF 檔案。 嘗試選取 PDF 中的文本,以確認其可搜尋性。 選擇功能還意味著可以在 PDF 檢視器中搜尋文字。 IronOCR 使用特定的字型在影像檔上覆蓋文字,可能會造成文字大小上的一些差異。 TIFF 文件 可搜索的 PDF 處理多頁文件 在處理多頁文件上的 PDF OCR 作業時,IronOCR 會依序處理每頁,並維持原始文件結構。 以下是將多頁掃描 PDF 轉換為可搜尋 PDF 的範例: using IronOcr; // Initialize IronTesseract with configuration var ocrTesseract = new IronTesseract { Configuration = { RenderSearchablePdf = true, PageSegmentationMode = TesseractPageSegmentationMode.Auto } }; // Load a multi-page PDF using var pdfInput = new OcrPdfInput("multi-page-scan.pdf"); // Optionally specify page range (e.g., pages 1-10) pdfInput.SelectPages(1, 10); // Perform OCR with progress tracking OcrResult result = ocrTesseract.Read(pdfInput); // Save as searchable PDF result.SaveAsSearchablePdf("searchable-multi-page.pdf"); // Display total pages processed Console.WriteLine($"Processed {result.Pages.Length} pages"); using IronOcr; // Initialize IronTesseract with configuration var ocrTesseract = new IronTesseract { Configuration = { RenderSearchablePdf = true, PageSegmentationMode = TesseractPageSegmentationMode.Auto } }; // Load a multi-page PDF using var pdfInput = new OcrPdfInput("multi-page-scan.pdf"); // Optionally specify page range (e.g., pages 1-10) pdfInput.SelectPages(1, 10); // Perform OCR with progress tracking OcrResult result = ocrTesseract.Read(pdfInput); // Save as searchable PDF result.SaveAsSearchablePdf("searchable-multi-page.pdf"); // Display total pages processed Console.WriteLine($"Processed {result.Pages.Length} pages"); Imports IronOcr ' Initialize IronTesseract with configuration Dim ocrTesseract As New IronTesseract With { .Configuration = New OcrConfiguration With { .RenderSearchablePdf = True, .PageSegmentationMode = TesseractPageSegmentationMode.Auto } } ' Load a multi-page PDF Using pdfInput As New OcrPdfInput("multi-page-scan.pdf") ' Optionally specify page range (e.g., pages 1-10) pdfInput.SelectPages(1, 10) ' Perform OCR with progress tracking Dim result As OcrResult = ocrTesseract.Read(pdfInput) ' Save as searchable PDF result.SaveAsSearchablePdf("searchable-multi-page.pdf") ' Display total pages processed Console.WriteLine($"Processed {result.Pages.Length} pages") End Using $vbLabelText $csharpLabel 在建立可搜尋的 PDF 時,如何套用篩選器? SaveAsSearchablePdf也接受一個布林標誌作為第二個參數,讓您可以對可搜尋的 PDF 套用篩選器或不套用篩選器,從而為開發人員提供選擇的靈活性。 使用影像最佳化篩選器可大幅提升 OCR 準確度,尤其是在處理低品質掃描時。 以下是應用灰階過濾器的範例,然後在 SaveAsSearchablePdf 的第二個參數中填入 true 來儲存具有過濾器的 PDF。 :path=/static-assets/ocr/content-code-examples/how-to/image-quality-correction-searchable-pdf.cs using IronOcr; var ocr = new IronTesseract(); var ocrInput = new OcrInput(); // Load a PDF file ocrInput.LoadPdf("invoice.pdf"); // Apply gray scale filter ocrInput.ToGrayScale(); OcrResult result = ocr.Read(ocrInput); // Save the result as a searchable PDF with filters applied result.SaveAsSearchablePdf("outputGrayscale.pdf", true); Imports IronOcr Dim ocr As New IronTesseract() Dim ocrInput As New OcrInput() ' Load a PDF file ocrInput.LoadPdf("invoice.pdf") ' Apply gray scale filter ocrInput.ToGrayScale() Dim result As OcrResult = ocr.Read(ocrInput) ' Save the result as a searchable PDF with filters applied result.SaveAsSearchablePdf("outputGrayscale.pdf", True) $vbLabelText $csharpLabel 為達到最佳效果,請考慮使用 過濾精靈 自動決定特定文件類型的最佳過濾器組合。 此工具會分析您的輸入,並建議適當的預處理步驟。 如何將可搜尋的 PDF 匯出為位元組或資料流? 也可以分別使用SaveAsSearchablePdfBytes和SaveAsSearchablePdfStream方法將可搜尋 PDF 的輸出作為位元組或流進行處理。 下面的程式碼範例展示如何使用這些方法。 :path=/static-assets/ocr/content-code-examples/how-to/searchable-pdf-searchable-pdf-byte-stream.cs // Export searchable PDF byte byte[] pdfByte = ocrResult.SaveAsSearchablePdfBytes(); // Export searchable PDF stream Stream pdfStream = ocrResult.SaveAsSearchablePdfStream(); ' Export searchable PDF byte Dim pdfByte() As Byte = ocrResult.SaveAsSearchablePdfBytes() ' Export searchable PDF stream Dim pdfStream As Stream = ocrResult.SaveAsSearchablePdfStream() $vbLabelText $csharpLabel 在與檔案系統存取可能受限的雲端儲存服務、資料庫或網路應用程式整合時,這些輸出選項尤其有用。 以下是一個顯示實際應用的延伸範例: 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 $vbLabelText $csharpLabel 效能考量 在處理大量文件時,請考慮實施 多執行緒 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 $vbLabelText $csharpLabel 進階組態選項 對於更進階的情境,您可以利用 詳細的 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 } $vbLabelText $csharpLabel 摘要 使用 IronOCR 創建可搜尋的 PDF 既直接又靈活。 無論您需要處理單一圖像、多頁文件或批次作業,這個函式庫都能提供強大的方法來產生各種格式的可搜尋 PDF。 以檔案、位元組或串流形式匯出的能力,使其可適用於任何應用程式架構,從桌上型電腦應用程式到雲端服務。 對於更進階的 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 中原始影像上方的隱形覆蓋層。這可確保準確的文字對圖像映射,讓使用者可以選擇和搜尋文字,同時保持原始文件的視覺外觀。這個函式庫使用專門的字型和定位演算法來達成這個目標。 Curtis Chau 立即與工程團隊聊天 技術撰稿人 Curtis Chau 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。 審核人 Jeffrey T. Fritz 首席計畫經理 - .NET 社群團隊 Jeff 也是 .NET 和 Visual Studio 團隊的首席計畫經理。他是 .NET Conf 虛擬會議系列的執行製作人,並主持「Fritz and Friends」開發人直播串流,每週播出兩次,與觀眾一起討論技術和編寫程式碼。Jeff 為 Microsoft Build、Microsoft Ignite、.NET Conf 和 Microsoft MVP Summit 等大型 Microsoft 開發人員活動撰寫工作坊、簡報和規劃內容。 準備好開始了嗎? Nuget 下載 5,384,824 | 版本: 2026.2 剛剛發布 免費 NuGet 下載 總下載量:5,384,824 查看許可證