How to Save Results as hOCR in an HTML File

by Chaknith Bin

hOCR, which stands for "HTML-based OCR," is a file format used to represent the results of Optical Character Recognition (OCR) in a structured manner. HOCR files are typically written in HTML (Hypertext Markup Language) and provide a way to store recognized text, layout information, and the coordinates of each recognized character within an image or document.

IronOCR provides a solution for performing optical character recognition on documents and exporting the results as hOCR in HTML format. It supports both HTML files and strings.

C# NuGet Library for OCR

Install with NuGet

Install-Package IronOcr

Download DLL

Download DLL

Manually install into your project

Export Result as hOCR Example

To export the result as hOCR, the user must first enable the Configuration.RenderSearchablePdfsAndHocr property by setting it to true. After obtaining the OCR result object from the Read method, use the SaveAsHocrFile method to export the OCR result as HTML. This method will output an HTML file that contains the reading result of the input documents. The code below demonstrates using the following sample TIFF file.

using IronOcr;

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

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

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

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

// Export as HTML
Imports IronOcr

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Enable render as hOCR
ocrTesseract.Configuration.RenderSearchablePdfsAndHocr = 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
VB   C#

Export Result as HTML String

Using the same TIFF sample image, you can utilize the SaveAsHocrString method to export the OCR result as an HTML string. This method will return an HTML string.

// Export as HTML string
string hocr = ocrResult.SaveAsHocrString();
' Export as HTML string
Dim hocr As String = ocrResult.SaveAsHocrString()
VB   C#

Chaknith Bin

Software Engineer

Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.