IRONSOFTWAREHOME

Save OCR Results as hOCR HTML in C# with IronOCR

Curtis Chau
Curtis Chau
Updated: August 2, 2026

IronOCR enables developers to save OCR results as hOCR HTML files by setting RenderHocr to true and using SaveAsHocrFile or SaveAsHocrString methods, preserving text layout and character coordinates in structured HTML format.

Quickstart: Save OCR Output as hOCR HTML File

Enable hOCR rendering and export results directly to an HTML file with one setup and one method call.

  1. 1Install IronOCR with NuGet Package Manager

    PM > Install-Package IronOcr

  2. 2Copy and run this code snippet.

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

    Start using IronOCR in your project today with a free trial
    arrow pointer

What Is hOCR and Why Use It?

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 written in HTML and provide a way to store recognized text, layout information, and the coordinates of each recognized character within an image or document. This structured format makes hOCR particularly valuable for applications requiring text position data, such as document indexing, accessibility tools, and advanced search implementations.

The hOCR format is essential for developers building applications that need to understand not just what text is present, but where that text appears on the original document. This spatial information enables features like highlighting text for debugging, creating clickable overlays on original images, and maintaining document layout integrity when converting scanned documents to accessible formats. For enterprise applications processing scanned documents, hOCR provides the foundation for advanced document understanding and extraction workflows.

How Do I Export OCR Results as hOCR Files?

To export the result as hOCR, first enable the Configuration.RenderHocr property by setting it to true. After obtaining the OcrResult object from the Read method, use the SaveAsHocrFile method to export the OCR result as HTML. This method outputs an HTML file containing 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.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");

The OcrInput class provides extensive options for preparing images before OCR processing. You can apply filters, specify regions of interest, and handle various input formats including multi-page TIFF files. When working with PDF OCR text extraction, the same hOCR export methods apply seamlessly.

Why Does Setting RenderHocr Matter?

Setting the RenderHocr property to true instructs IronOCR to generate the necessary hOCR structure during the OCR process. Without this configuration, the SaveAsHocrFile and SaveAsHocrString methods won't produce properly formatted hOCR output with layout preservation. This configuration must be set before calling the Read method, as it affects how the Tesseract engine processes and structures the output data.

The hOCR format preserves crucial metadata including:

  • Character-level bounding boxes
  • Word confidence scores
  • Line and paragraph structure
  • Page dimensions and DPI information
  • Font characteristics when detectable

This metadata is particularly useful when implementing computer vision workflows or building systems that need to understand document structure beyond simple text extraction.

What File Types Support hOCR Export?

IronOCR supports hOCR export from various image formats including TIFF, PNG, JPEG, BMP, and GIF. PDF documents can also be processed and exported as hOCR, with each page's text and layout information preserved in the HTML structure. The library handles both single-page images and multi-page documents seamlessly.

For optimal results with different file types:

  • TIFF: Ideal for scanned documents, supports multi-page processing
  • PDF: Excellent for mixed content (text and images)
  • PNG/JPEG: Best for photographs or screenshots requiring OCR
  • BMP: Uncompressed format suitable for high-quality scans

When dealing with specialized document types like passports or license plates, the hOCR format helps preserve the spatial relationships between different text elements, making it easier to extract specific fields based on their location.

How Can I Export OCR Results as HTML Strings?

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

// Export as HTML string
string hocr = ocrResult.SaveAsHocrString();

The string output contains complete hOCR markup that can be further processed, stored in databases, or integrated into web applications. This approach is particularly useful when building searchable PDF systems or implementing custom document indexing solutions. For developers working with 125 international languages, the hOCR format preserves language-specific text attributes and reading direction information.

When Should I Use String Output Instead of Files?

String output is ideal when you need to process or manipulate the hOCR data in memory, integrate with web services, or store results in a database. This approach avoids file system dependencies and enables dynamic HTML generation for web applications. Common use cases include:

  • Web API Integration: Return hOCR data directly in API responses
  • Database Storage: Store OCR results with document metadata
  • Real-time Processing: Process results without disk I/O overhead
  • Cloud Functions: Work within serverless environments with limited file access
  • Content Management Systems: Integrate OCR results into existing document workflows

For applications requiring progress tracking, string output allows immediate processing of partial results as they become available. This is particularly beneficial when implementing multithreaded OCR processing where multiple documents are processed concurrently.

How Do I Process Multiple Pages to HTML Strings?

When working with multi-page documents, SaveAsHocrString consolidates all pages into a single HTML string with proper page divisions. Each page's content is wrapped in appropriate hOCR elements, maintaining the document structure and page boundaries.

// Processing multi-page documents
using var multiPageInput = new OcrPdfInput("multi-page-document.pdf");
multiPageInput.Title = "Multi-Page Document";

 // Configure for hOCR output
 IronTesseract tesseract = new IronTesseract();
 tesseract.Configuration.RenderHocr = true;

 // Read all pages
 OcrResult result = tesseract.Read(multiPageInput);

 // Export as single HTML string with all pages
 string fullHocr = result.SaveAsHocrString();
C#

This approach works seamlessly with PDF streams and supports advanced scenarios like processing specific page ranges or applying different OCR configurations to different pages.

Advanced hOCR Implementation Tips

What Are Best Practices for hOCR Output Quality?

To maximize the quality of your hOCR output, consider applying image optimization filters before processing:

var input = new OcrImageInput("document.png");
input.DeNoise();  // Remove image noise
input.Deskew();   // Correct image rotation
input.Scale(2);   // Upscale for better recognition

IronTesseract ocr = new IronTesseract();
ocr.Configuration.RenderHocr = true;
var result = ocr.Read(input);

For low quality scans, additional preprocessing steps can significantly improve hOCR accuracy. The filter wizard helps determine optimal filter combinations for your specific document types.

How Does hOCR Structure Support Advanced Processing?

The generated hOCR follows the standard specification with nested div elements representing the document hierarchy:

<div class='ocr_page' title='bbox 0 0 2480 3508'>
  <div class='ocr_carea' title='bbox 156 114 2324 3395'>
    <p class='ocr_par' title='bbox 157 114 2323 164'>
      <span class='ocr_line' title='bbox 157 114 2323 164'>
        <span class='ocr_word' title='bbox 157 114 294 161'>Hello</span>
        <span class='ocr_word' title='bbox 334 119 483 161'>World</span>
      </span>
    </p>
  </div>
</div>
HTML

This structure enables precise text location extraction and advanced document analysis capabilities, making it valuable for applications requiring spatial text relationships or layout preservation. When working with table extraction, the hOCR format helps maintain the tabular structure and cell relationships.

The bbox (bounding box) attributes contain coordinates in the format "bbox left top right bottom", providing pixel-precise location data for each text element. This information is crucial for:

  • Creating interactive document viewers with text selection
  • Implementing redaction systems that preserve layout
  • Building accessibility tools that maintain reading order
  • Developing document comparison systems

For developers requiring even more detailed configuration options, the Tesseract detailed configuration guide provides advanced settings that affect hOCR output quality and structure.

RenderHocr

Frequently Asked Questions

What is hOCR in the context of OCR and why should it be used?

hOCR is a file format used to represent OCR results in a structured manner, combining HTML with recognized text, layout information, and character coordinates. It is valuable for applications needing precise text positioning data, like document indexing and maintaining layout integrity.

How can I save OCR results as hOCR files in C# using IronOCR?

To save OCR results as hOCR files in C#, you should set the `RenderHocr` property to true in IronOCR, then use the `SaveAsHocrFile` method to output the results as an HTML file. This process preserves the text layout and coordinates in the output.

What methods does IronOCR provide for exporting OCR results to hOCR HTML?

IronOCR provides the `SaveAsHocrFile` method for exporting results to an HTML file and the `SaveAsHocrString` method for obtaining an HTML string, both of which support preserving text layout and character coordinates.

Why is the `RenderHocr` configuration important in IronOCR?

The `RenderHocr` configuration is crucial because it enables IronOCR to format OCR output as hOCR, which includes layout and spatial information. Without it, the output would not maintain the structured format necessary for advanced document processing.

Can IronOCR handle multiple image formats for hOCR export?

Yes, IronOCR supports hOCR export from various image formats including TIFF, PNG, JPEG, BMP, and GIF, as well as PDF documents, preserving text and layout information across different media types.

What are the advantages of exporting OCR results as HTML strings?

Exporting OCR results as HTML strings allows for in-memory processing, integration with APIs, and storage in databases without file dependencies. This approach is beneficial for real-time applications, search systems, and cloud-based services.

How does hOCR format support advanced document processing tasks?

hOCR's structured format with bounding box data allows for precise text location extraction, enabling features like interactive text viewers, layout-preserving redaction, and accessibility tools. It supports advanced tasks requiring spatial text relationships.

What preprocessing steps improve hOCR output quality in IronOCR?

Applying image optimization filters such as de-noising, deskewing, and upscaling before OCR processing can significantly enhance hOCR output quality, especially for low-quality scans or complex document formats.

Which scenarios benefit from using HTML string output instead of file output in OCR?

HTML string output is ideal for scenarios requiring immediate processing in memory, such as web API integration, database storage, real-time analysis, and serverless applications lacking file system access.

How does IronOCR handle multi-page documents when exporting to hOCR?

IronOCR consolidates OCR results from multi-page documents into a single HTML string or file with appropriate page divisions, maintaining document structure and content continuity across pages.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...
Read More

Ready to Get Started?

Nuget Downloads 6,236,385Version:2026.9just released

Get your FREE

30-day Trial Key instantly.

bullet_checkedNo credit card or account creation required
bullet_testTest in production
without watermarks
bullet_calendar30 days fully
functional product
bullet_support24/5 technical
support during trial
Get your free 30-day Trial Key instantly.
No credit card or account creation required
C# NuGet Library for PDF
Install with NuGet

Version: 2026.9

PM > Install-Package IronOcr
nuget.org/packages/IronOcr/
  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search "IronOCR"
  3. Select the package and install
C# PDF DLL
Download DLL

Version: 2026.9

or download Windows Installer here.

  1. Download and unzip IronOCR to a location such as ~/Libs within your Solution directory
  2. In Visual Studio Solution Explorer, right click References. Select Browse, "IronOCR.dll"

Licenses from $999

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required