IronOCR 操作指南 將文字高亮顯示為圖片 Highlight Texts as Images in C# with IronOCR 使用 IronOcr 在 C# 中將文字高亮顯示為影像。 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 的 HighlightTextAndSaveAsImages 方法通過在檢測到的文本(字符、單詞、行或段落)周圍繪製邊界框來可視化 OCR 結果,並將其保存為診斷圖像,使開發人員能夠驗證 OCR 精度並調試識別問題。 可視化 OCR 結果包括在引擎偵測到的影像中特定文字元素周圍渲染邊界框。 此流程會在個別字元、字詞、行或段落的精確位置上覆蓋明顯的高亮點,提供清晰的認知內容地圖。 此視覺化回饋對於調試和驗證 OCR 輸出的準確性至關重要,可顯示軟體已識別出哪些內容以及在哪些地方出錯。 在處理複雜的文件或排除識別問題時,可視化高亮顯示成為重要的診斷工具。 本文利用 HighlightTextAndSaveAsImages 方法展示 IronOCR 的診斷功能。 此功能可強調特定的文字部分,並將其儲存為影像以供驗證。 無論是建立文件處理系統、執行品質控制措施,或是驗證您的 OCR 實作,此功能都能立即提供 OCR 引擎偵測到的視覺回饋。 快速入門:立即高亮顯示 PDF 中的單字 此片段示範了 IronOCR 的用法:載入 PDF,並高亮顯示文件中的每個字,將結果儲存為影像。 只需一行即可獲得 OCR 結果的視覺回饋。 立即開始使用 NuGet 建立 PDF 檔案: 使用 NuGet 套件管理器安裝 IronOCR PM > Install-Package IronOcr 複製並運行這段程式碼。 new IronOcr.OcrInput().LoadPdf("document.pdf").HighlightTextAndSaveAsImages(new IronOcr.IronTesseract(), "highlight_page_", IronOcr.ResultHighlightType.Word); 部署到您的生產環境進行測試 立即開始在您的專案中使用 IronOCR,免費試用! 免費試用30天 ### 最小工作流程(5 個步驟) 下載一個 C# 庫以檢測頁面旋轉 實例化 OCR 引擎 使用LoadPdf載入 PDF 文檔 使用HighlightTextAndSaveAsImages可以高亮顯示文字片段並將其儲存為圖像。 如何高亮顯示文字並另存影像? <! -- --> <!--說明:顯示逐步過程的截圖 --> 使用 IronOCR 可以直接高亮顯示文字並將其儲存為影像。 使用 LoadPdf 載入現有的 PDF,然後調用 HighlightTextAndSaveAsImages 方法來突出顯示文字部分並將其儲存為影像。 此技術可驗證 OCR 的準確性,並除錯文件中的文字辨識問題。 此方法需要三個參數:IronTesseract OCR 引擎、輸出檔名的前綴,以及 ResultHighlightType 的枚舉,該枚舉會決定要高亮顯示的文字類型。 本範例使用 ResultHighlightType.Paragraph 將文字區塊高亮為段落。 請注意這個函式使用輸出字串前綴,並為每個頁面的輸出影像檔案名稱附加頁面識別碼 (例如 "page_0", "page_1") @。 本範例使用的 PDF 有三個段落。 輸入的 PDF 是什麼樣子? 我該如何實作高亮度程式碼? 以下範例程式碼展示了使用 OcrInput 類別的基本實作。 :path=/static-assets/ocr/content-code-examples/how-to/highlight-texts-as-images.cs using IronOcr; IronTesseract ocrTesseract = new IronTesseract(); using var ocrInput = new OcrInput(); ocrInput.LoadPdf("document.pdf"); ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_page_", ResultHighlightType.Paragraph); Imports IronOcr Private ocrTesseract As New IronTesseract() Private ocrInput = New OcrInput() ocrInput.LoadPdf("document.pdf") ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_page_", ResultHighlightType.Paragraph) $vbLabelText $csharpLabel 輸出影像會顯示什麼? 如上圖所示,所有三個段落都已用淺紅色方塊突出顯示。 此視覺化表示法可協助開發人員快速辨識 OCR 引擎如何將文件分割成可閱讀的區塊。 有哪些不同的 ResultHighlightType 選項? 上面的範例使用 ResultHighlightType.Paragraph 來突出顯示文字區塊。 IronOCR 透過此枚舉提供額外的突出顯示選項。 以下是可用類型的完整清單,每種類型都有不同的診斷用途。 字元:在 OCR 引擎偵測到的每個單一字元周圍繪出一個邊框。對於調試字元識別或專用字型非常有用,尤其是在處理 自訂語言檔案時。 Word: 高亮顯示引擎所辨識的每個完整字詞。是驗證字詞邊界和正確識別字詞的理想工具,尤其是在實施 條碼和 QR 讀取與文字識別同時進行時。 Line:高亮顯示檢測到的每一行文字。對於具有複雜佈局、需要行識別驗證的文件非常有用,例如在處理 掃描文件時。 段落:高亮顯示以段落為群組的整個文字區塊。非常適合了解文件排版和驗證文字區塊分割,在使用 表格萃取時特別有用。 如何比較不同的高亮類型? 此綜合範例示範在同一份文件上產生所有不同類型的重點,讓您可以比較結果: using IronOcr; using System; // Initialize the OCR engine with custom configuration IronTesseract ocrTesseract = new IronTesseract(); // Configure for better accuracy if needed ocrTesseract.Configuration.ReadBarCodes = false; // Disable if not needed for performance ocrTesseract.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.AutoOsd; // Load the PDF document using var ocrInput = new OcrInput(); ocrInput.LoadPdf("document.pdf"); // Generate highlights for each type Console.WriteLine("Generating character-level highlights..."); ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_character_", ResultHighlightType.Character); Console.WriteLine("Generating word-level highlights..."); ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_word_", ResultHighlightType.Word); Console.WriteLine("Generating line-level highlights..."); ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_line_", ResultHighlightType.Line); Console.WriteLine("Generating paragraph-level highlights..."); ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_paragraph_", ResultHighlightType.Paragraph); Console.WriteLine("All highlight images have been generated successfully!"); using IronOcr; using System; // Initialize the OCR engine with custom configuration IronTesseract ocrTesseract = new IronTesseract(); // Configure for better accuracy if needed ocrTesseract.Configuration.ReadBarCodes = false; // Disable if not needed for performance ocrTesseract.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.AutoOsd; // Load the PDF document using var ocrInput = new OcrInput(); ocrInput.LoadPdf("document.pdf"); // Generate highlights for each type Console.WriteLine("Generating character-level highlights..."); ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_character_", ResultHighlightType.Character); Console.WriteLine("Generating word-level highlights..."); ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_word_", ResultHighlightType.Word); Console.WriteLine("Generating line-level highlights..."); ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_line_", ResultHighlightType.Line); Console.WriteLine("Generating paragraph-level highlights..."); ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_paragraph_", ResultHighlightType.Paragraph); Console.WriteLine("All highlight images have been generated successfully!"); Imports IronOcr Imports System ' Initialize the OCR engine with custom configuration Dim ocrTesseract As New IronTesseract() ' Configure for better accuracy if needed ocrTesseract.Configuration.ReadBarCodes = False ' Disable if not needed for performance ocrTesseract.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.AutoOsd ' Load the PDF document Using ocrInput As New OcrInput() ocrInput.LoadPdf("document.pdf") ' Generate highlights for each type Console.WriteLine("Generating character-level highlights...") ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_character_", ResultHighlightType.Character) Console.WriteLine("Generating word-level highlights...") ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_word_", ResultHighlightType.Word) Console.WriteLine("Generating line-level highlights...") ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_line_", ResultHighlightType.Line) Console.WriteLine("Generating paragraph-level highlights...") ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_paragraph_", ResultHighlightType.Paragraph) End Using Console.WriteLine("All highlight images have been generated successfully!") $vbLabelText $csharpLabel 如何處理多頁文件? 在處理多頁 PDF 或 多格 TIFF 檔案時,高亮顯示功能會自動個別處理每一頁。 這在執行 PDF OCR 文字擷取工作流程時特別有用: using IronOcr; using System.IO; IronTesseract ocrTesseract = new IronTesseract(); // Load a multi-page document using var ocrInput = new OcrInput(); ocrInput.LoadPdf("multi-page-document.pdf"); // Create output directory if it doesn't exist string outputDir = "highlighted_pages"; Directory.CreateDirectory(outputDir); // Generate highlights for each page // Files will be named: highlighted_pages/page_0.png, page_1.png, etc. ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, Path.Combine(outputDir, "page_"), ResultHighlightType.Word); // Count generated files for verification int pageCount = Directory.GetFiles(outputDir, "page_*.png").Length; Console.WriteLine($"Generated {pageCount} highlighted page images"); using IronOcr; using System.IO; IronTesseract ocrTesseract = new IronTesseract(); // Load a multi-page document using var ocrInput = new OcrInput(); ocrInput.LoadPdf("multi-page-document.pdf"); // Create output directory if it doesn't exist string outputDir = "highlighted_pages"; Directory.CreateDirectory(outputDir); // Generate highlights for each page // Files will be named: highlighted_pages/page_0.png, page_1.png, etc. ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, Path.Combine(outputDir, "page_"), ResultHighlightType.Word); // Count generated files for verification int pageCount = Directory.GetFiles(outputDir, "page_*.png").Length; Console.WriteLine($"Generated {pageCount} highlighted page images"); Imports IronOcr Imports System.IO Dim ocrTesseract As New IronTesseract() ' Load a multi-page document Using ocrInput As New OcrInput() ocrInput.LoadPdf("multi-page-document.pdf") ' Create output directory if it doesn't exist Dim outputDir As String = "highlighted_pages" Directory.CreateDirectory(outputDir) ' Generate highlights for each page ' Files will be named: highlighted_pages/page_0.png, page_1.png, etc. ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, Path.Combine(outputDir, "page_"), ResultHighlightType.Word) ' Count generated files for verification Dim pageCount As Integer = Directory.GetFiles(outputDir, "page_*.png").Length Console.WriteLine($"Generated {pageCount} highlighted page images") End Using $vbLabelText $csharpLabel 什麼是效能最佳實作? 使用高亮顯示功能時,請考慮這些最佳實務: 1.檔案大小:高亮影像可能會很大,尤其是高解析度的文件。 在處理大量批次時,請考慮輸出目錄的可用空間。 有關最佳化技巧,請參閱我們的 快速 OCR 配置指南。 2.效能:產生重點會增加處理開銷。 對於只是偶爾需要高亮度的生產系統,請將其作為獨立的診斷流程來實施,而不是主要工作流程的一部分。 考慮使用多執行緒 OCR 進行批次處理。 3.錯誤處理:在處理檔案作業時,請務必執行適當的錯誤處理: try { using var ocrInput = new OcrInput(); ocrInput.LoadPdf("document.pdf"); // Apply image filters if needed for better recognition ocrInput.Deskew(); // Correct slight rotations ocrInput.DeNoise(); // Remove background noise ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_", ResultHighlightType.Word); } catch (Exception ex) { Console.WriteLine($"Error during highlighting: {ex.Message}"); // Log error details for debugging } try { using var ocrInput = new OcrInput(); ocrInput.LoadPdf("document.pdf"); // Apply image filters if needed for better recognition ocrInput.Deskew(); // Correct slight rotations ocrInput.DeNoise(); // Remove background noise ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_", ResultHighlightType.Word); } catch (Exception ex) { Console.WriteLine($"Error during highlighting: {ex.Message}"); // Log error details for debugging } Imports System Try Using ocrInput As New OcrInput() ocrInput.LoadPdf("document.pdf") ' Apply image filters if needed for better recognition ocrInput.Deskew() ' Correct slight rotations ocrInput.DeNoise() ' Remove background noise ocrInput.HighlightTextAndSaveAsImages(ocrTesseract, "highlight_", ResultHighlightType.Word) End Using Catch ex As Exception Console.WriteLine($"Error during highlighting: {ex.Message}") ' Log error details for debugging End Try $vbLabelText $csharpLabel 高亮顯示如何與 OCR 結果整合? 高亮功能可與 IronOCR 的 result 物件無縫配合,讓您可以將視覺化的高亮內容與擷取的文字資料相互關聯。 當您需要 追蹤 OCR 進度 或驗證已識別文字的特定區段時,此功能尤其有用。 OcrResult 類提供每個偵測到的元素的詳細資訊,這些資訊直接對應此方法所產生的視覺重點。 如果我遇到問題怎麼辦? 如果遇到高亮顯示功能的問題,請參閱 一般疑難排解指南,以瞭解常見的解決方案。 針對特定的高亮度相關問題: 空白輸出影像:確保輸入文件包含可讀的文字,且 OCR 引擎已針對您的文件類型進行適當設定。 您可能需要套用 影像最佳化濾鏡 或 修正影像方向,以提高辨識度。 遺漏重點:某些文件類型可能需要特定的預處理。 嘗試套用 影像濾鏡 或 修正影像方向,以提高辨識度。 效能問題:對於大型文件,請考慮實施 多執行緒 以提高處理速度。 此外,如果使用品質不佳的輸入,請檢閱我們的 修正低品質掃描指南。 我如何將它用於生產調試? 高亮顯示功能可作為絕佳的生產除錯工具。 當與用於長時間執行作業的 abort tokens 和 timeouts 整合時,您可以建立一個強大的診斷系統。 考慮在您的應用程式中實作除錯模式: public class OcrDebugger { private readonly IronTesseract _tesseract; private readonly bool _debugMode; public OcrDebugger(bool enableDebugMode = false) { _tesseract = new IronTesseract(); _debugMode = enableDebugMode; } public OcrResult ProcessDocument(string filePath) { using var input = new OcrInput(); input.LoadPdf(filePath); // Apply preprocessing input.Deskew(); input.DeNoise(); // Generate debug highlights if in debug mode if (_debugMode) { string debugPath = $"debug_{Path.GetFileNameWithoutExtension(filePath)}_"; input.HighlightTextAndSaveAsImages(_tesseract, debugPath, ResultHighlightType.Word); } // Perform actual OCR return _tesseract.Read(input); } } public class OcrDebugger { private readonly IronTesseract _tesseract; private readonly bool _debugMode; public OcrDebugger(bool enableDebugMode = false) { _tesseract = new IronTesseract(); _debugMode = enableDebugMode; } public OcrResult ProcessDocument(string filePath) { using var input = new OcrInput(); input.LoadPdf(filePath); // Apply preprocessing input.Deskew(); input.DeNoise(); // Generate debug highlights if in debug mode if (_debugMode) { string debugPath = $"debug_{Path.GetFileNameWithoutExtension(filePath)}_"; input.HighlightTextAndSaveAsImages(_tesseract, debugPath, ResultHighlightType.Word); } // Perform actual OCR return _tesseract.Read(input); } } Imports System.IO Public Class OcrDebugger Private ReadOnly _tesseract As IronTesseract Private ReadOnly _debugMode As Boolean Public Sub New(Optional enableDebugMode As Boolean = False) _tesseract = New IronTesseract() _debugMode = enableDebugMode End Sub Public Function ProcessDocument(filePath As String) As OcrResult Using input As New OcrInput() input.LoadPdf(filePath) ' Apply preprocessing input.Deskew() input.DeNoise() ' Generate debug highlights if in debug mode If _debugMode Then Dim debugPath As String = $"debug_{Path.GetFileNameWithoutExtension(filePath)}_" input.HighlightTextAndSaveAsImages(_tesseract, debugPath, ResultHighlightType.Word) End If ' Perform actual OCR Return _tesseract.Read(input) End Using End Function End Class $vbLabelText $csharpLabel 我下一步該往哪裡走? <! -- --> <!--說明:說明程式碼概念的圖表或截圖 --> 現在您已瞭解如何使用高亮顯示功能,請探索: 從您的 OCR 結果建立可搜尋的 PDF</a 閱讀特定文件類型,例如護照或執照 使用我們的 入門指南在您的開發環境中設定 IronOCR 為全球應用程式實施 125 種國際語言支援 使用濾鏡精靈優化影像處理 對於生產使用,請記得取得授權以移除水印並存取完整功能。 常見問題解答 如何在 C# 應用程式中視覺化 OCR 結果? IronOCR 提供 HighlightTextAndSaveAsImages 方法,可透過在偵測到的文字元素(字元、單字、行或段落)周圍繪製邊界框來顯示 OCR 結果,並將其儲存為診斷影像。此功能可協助開發人員驗證 OCR 的精確度和除錯識別問題。 在 PDF 文件中高亮顯示字詞的最簡單方法是什麼? 有了 IronOCR,您只需要一行程式碼就可以高亮 PDF 中的字:new IronOcr.OcrInput().LoadPdf("document.pdf").HighlightTextAndSaveAsImages(new IronOcr.IronTesseract(), "highlight_page_", IronOcr.ResultHighlightType.Word)。這會載入 PDF 並建立高亮文字的影像。 HighlightTextAndSaveAsImages 方法需要哪些參數? IronOCR 中的 HighlightTextAndSaveAsImages 方法需要三個參數:IronTesseract OCR 引擎實例、輸出檔名的前綴字串,以及 ResultHighlightType 枚舉值(指定要高亮哪些文字元素(字元、單字、行或段落)))。 使用文字高亮時,輸出影像如何命名? IronOCR 會將您指定的前綴與頁面識別碼結合,自動為輸出圖片命名。例如,如果您使用「highlight_page_」作為前綴,該方法會為文件中的每一頁產生命名為「highlight_page_0」、「highlight_page_1」等的檔案。 為什麼視覺高亮對 OCR 開發很重要? IronOCR 中的可視化高亮顯示提供了關鍵的診斷回饋,它可以準確顯示 OCR 引擎檢測到了哪些文字以及哪些地方出現了潛在錯誤。此視覺化地圖可協助開發人員調試辨識問題、驗證 OCR 準確性,以及排除複雜文件中的問題。 除了文字之外,我可以強調不同類型的文字元素嗎? 是的,IronOCR 的 ResultHighlightType 枚舉允許您高亮顯示各種文字元素,包括單獨的 Characters、Words、Lines 或整個段落。只需在呼叫 HighlightTextAndSaveAsImages 方法時指定所需的類型,即可視覺化不同層級的文字偵測。 Curtis Chau 立即與工程團隊聊天 技術撰稿人 Curtis Chau 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。 準備好開始了嗎? Nuget 下載 5,384,824 | 版本: 2026.2 剛剛發布 免費 NuGet 下載 總下載量:5,384,824 查看許可證