如何將結果以 hOCR 格式儲存到 HTML 檔案中 | IronOCR

How to Save Results as hOCR in an HTML File

This article was translated from English: Does it need improvement?
Translated
View the article in English

hOCR,全名為「基於 HTML 的 OCR」,是一種用於以結構化方式表示光學字符識別 (OCR) 結果的文件格式。 HOCR 文件通常是用 HTML (超文本標記語言) 編寫的,提供了一種存儲識別文本、佈局信息以及圖像或文檔中每個識別字符座標的方法。

快速入門:將 OCR 輸出保存為 hOCR HTML 文件

這是使用 IronOCR 的最簡單方法:啟用 hOCR 渲染並只需一個設置和一個調用,即可直接將結果導出到 HTML 文件。 開發者可以快速啟動,並在片刻內以可用的 HTML 標記查看 OCR 結果。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronOCR with NuGet Package Manager

    PM > Install-Package IronOcr

  2. Copy and run this code snippet.

    var hocr = new IronTesseract { Configuration = { RenderHocr = true } }.Read(new OcrInput("image.png")).SaveAsHocrString();
  3. Deploy to test on your live environment

    Start using IronOCR in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小工作流程(5 步驟)

  1. 下載 C# 庫以將結果另存為 hOCR 到 HTML 文件中
  2. 準備目標圖像和 PDF 文件
  3. 設置 RenderHocr 屬性為 true
  4. 使用 SaveAsHocrFile 方法輸出 HTML 文件
  5. 使用 SaveAsHocrString 方法輸出 HTML 字串


導出結果為 hOCR 示例

要導出結果為 hOCR,用戶必須首先通過將 Configuration.RenderHocr 屬性設為 true 來啟用。 在從 Read 方法獲得 OCR 結果對象後,使用 SaveAsHocrFile 方法將 OCR 結果導出為 HTML。 此方法將輸出包含輸入文檔閱讀結果的 HTML 文件。 下面的代碼演示了使用以下示例 TIFF文件。

:path=/static-assets/ocr/content-code-examples/how-to/html-export-export-html.cs
using IronOcr;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Enable render as hOCR
ocrTesseract.Configuration.RenderHocr = true;

// Add image
using var imageInput = new OcrImageInput("Potter.tiff");
imageInput.Title = "Html Title";

// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);

// Export as HTML
ocrResult.SaveAsHocrFile("result.html");
Imports IronOcr

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Enable render as hOCR
ocrTesseract.Configuration.RenderHocr = True

' Add image
Dim imageInput = New OcrImageInput("Potter.tiff")
imageInput.Title = "Html Title"

' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)

' Export as HTML
ocrResult.SaveAsHocrFile("result.html")
$vbLabelText   $csharpLabel

導出結果為 HTML 字串

使用相同的 TIFF 示例圖像,您可以使用 SaveAsHocrString 方法將 OCR 結果導出為 HTML 字串。 此方法將返回一個 HTML 字串。

:path=/static-assets/ocr/content-code-examples/how-to/html-export-export-html-string.cs
// Export as HTML string
string hocr = ocrResult.SaveAsHocrString();
' Export as HTML string
Dim hocr As String = ocrResult.SaveAsHocrString()
$vbLabelText   $csharpLabel

常見問題解答

什麼是hOCR?它有什麼用途?

hOCR 代表“基於 HTML 的 OCR”,它是一種用於以結構化方式表示光學字元辨識結果的檔案格式。它用於儲存識別出的文字、佈局資訊以及圖像或文件中每個字元的座標,通常以 HTML 格式儲存。

如何使用 C# 將 OCR 結果儲存為 hOCR 檔案?

您可以使用 IronOCR 將 OCR 結果儲存為 hOCR 文件,方法是先將RenderHocr屬性設為 true,然後使用SaveAsHocrFile方法將結果輸出為 HTML 文件。

OCR辨識結果可以匯出為HTML字串嗎?

是的,可以使用 IronOCR 將 OCR 結果匯出為 HTML 字串,只需將RenderHocr屬性設為 true 並使用SaveAsHocrString方法,該方法會以 HTML 字串格式傳回 OCR 結果。

IronOCR是否支援對影像和PDF進行OCR處理?

IronOCR 支援對影像和 PDF 文件進行 OCR 處理,讓使用者可以將結果匯出為 HTML 格式的 hOCR。

在 C# 中將 OCR 結果匯出為 hOCR 需要哪些步驟?

若要使用 C# 將 OCR 結果匯出為 hOCR,請下載 IronOCR 庫,準備圖像或 PDF 文件,將RenderHocr屬性設為 true,然後使用SaveAsHocrFileSaveAsHocrString匯出結果。

IronOCR 適用於哪種程式設計環境?

IronOCR 適用於 .NET C# 程式設計環境,因此是使用 C# 的開發人員的理想選擇。

是否有將 OCR 結果儲存為 hOCR 的範例程式碼?

是的,IronOCR 提供了範例 C# 程式碼,示範如何從圖像檔案中讀取文字並將 OCR 結果儲存為 hOCR 檔案或字串,並提供了使用範例 TIFF 檔案的實際範例。

IronOCR 中如何使用 RenderHocr 屬性?

IronOCR 中的RenderHocr屬性用於啟用以 hOCR 格式匯出 OCR 結果。將其設為 true 後,結果可以匯出為 HTML 檔案或字串。

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'name'

Filename: sections/author_component.php

Line Number: 18

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 18
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'title'

Filename: sections/author_component.php

Line Number: 38

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 38
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'comment'

Filename: sections/author_component.php

Line Number: 48

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 48
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

審核人

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'name'

Filename: sections/author_component.php

Line Number: 70

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 70
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'title'

Filename: sections/author_component.php

Line Number: 84

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 84
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'comment'

Filename: sections/author_component.php

Line Number: 85

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 85
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

準備好開始了嗎?
Nuget 下載 5,044,537 | 版本: 2025.11 剛剛發布