How to Save Results as hOCR in an HTML File

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.

Get started with IronOCR

Start using IronOCR in your project today with a free trial.

First Step:
green arrow pointer



Export Result as hOCR Example

To export the result as hOCR, the user must first enable the Configuration.RenderHocr 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.

: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

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.

: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

Frequently Asked Questions

What is hOCR?

hOCR stands for 'HTML-based OCR,' a file format used to represent the results of Optical Character Recognition in a structured manner. It is typically written in HTML and stores recognized text, layout information, and coordinates of each character within an image or document.

How does IronOCR support hOCR?

IronOCR allows users to perform optical character recognition on documents and export the results as hOCR in HTML format. It supports both HTML files and strings.

What steps are needed to save results as hOCR in an HTML file using IronOCR?

First, download the IronOCR C# library. Prepare your image or PDF document, set the RenderHocr property to true, and then use the SaveAsHocrFile method to output an HTML file.

How can I export OCR results as an HTML string?

To export OCR results as an HTML string, set the RenderHocr property to true and use the SaveAsHocrString method. This method will return the OCR result as an HTML string.

Can IronOCR export OCR results from both images and PDFs?

Yes, IronOCR can process both images and PDF documents to perform OCR and export the results as hOCR in HTML format.

What programming language is required to use IronOCR?

IronOCR is used with .NET C#, making it suitable for developers working within the C# programming environment.

Is there an example code to save OCR results as hOCR using IronOCR?

Yes, the documentation provides example C# code demonstrating how to use IronOCR to read text from an image file and save the OCR results as an hOCR file.

What is the RenderHocr property used for?

The RenderHocr property is used to enable or disable the output of OCR results in the hOCR format. It should be set to true to export results as hOCR.

Chaknith related to Export Result as HTML String
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.
Reviewed by
Jeff Fritz
Jeffrey T. Fritz
Principal Program Manager - .NET Community Team
Jeff is also a Principal Program Manager for the .NET and Visual Studio teams. He is the executive producer of the .NET Conf virtual conference series and hosts 'Fritz and Friends' a live stream for developers that airs twice weekly where he talks tech and writes code together with viewers. Jeff writes workshops, presentations, and plans content for the largest Microsoft developer events including Microsoft Build, Microsoft Ignite, .NET Conf, and the Microsoft MVP Summit