Skip to content
ComPDF

OCR

Overview

OCR (Optical Character Recognition) is the process of converting images of typed, handwritten, or printed text into machine-encoded text.

OCR is commonly used for text recognition and extraction from the following types of documents:

  • Non-editable scanned PDF files
  • Photographs of documents.
  • Scene photos such as advertising layouts, signboards, etc.
  • Identification cards, passports, vehicle license plates, and other official plates.
  • Invoices, bills, receipts, and other financial documents.

The following features support OCR:

  • PDF to Word
  • PDF to Excel
  • PDF to PowerPoint (PPT)
  • PDF to HTML
  • PDF to Rich Text Format (RTF)
  • PDF to Text (TXT)
  • PDF to CSV
  • PDF to Searchable PDF
  • PDF to OFD
  • Extract PDF to JSON
  • Extract PDF to Markdown

OCR Language Support of ComPDF Conversion SDK:

Script / NotesLanguage (Native)Language (In English)
Latn; AmericanEnglishEnglish
Latn; CanadianFrançais canadienFrench
Hans/Hant中文简体Chinese (Simplified)
Hans/Hant中文繁体Chinese (Traditional)
Jpan日本語Japanese
Kore한국어Korean
LatnDeutschGerman
LatnСрпски (латиница)Serbian (latin)
LatnOccitan, lenga d'òc, provençalOccitan
LatnDanskDanish
LatnItalianoItalian
Latn; EuropeanEspañolSpanish
Latn; EuropeanPortuguês (Portugal)Portuguese
LatnTe reo MāoriMaori
LatnBahasa MelayuMalay
LatnMaltiMaltese
LatnNederlandsDutch
Latn; BokmålNorskNorwegian
LatnPolskiPolish
LatnRomânăRomanian
LatnSlovenčinaSlovak
LatnSlovenščinaSlovenian
LatnshqipAlbanian
LatnSvenskaSwedish
LatnSwahiliSwahili
LatnWikang TagalogTagalog
LatnTürkçeTurkish
LatnoʻzbekchaUzbek
LatnTiếng ViệtVietnamese
LatnAfrikaansAfrikaans
LatnAzərbaycanAzerbaijani
LatnBosanskiBosnian
LatnČeštinaCzech
LatnCymraegWelsh
LatnEesti keelEstonian
LatnGaeilgeIrish
LatnHrvatskiCroatian
LatnMagyarHungarian
LatnBahasa IndonesiaIndonesian
LatnÍslenskaIcelandic
LatnKurdîKurdish
LatnLietuviųLithuanian
LatnLatviešuLatvian

Set OCR Language

In the current mainline version, OCR languages should be passed through the Languages for each conversion task, rather than through a separate global interface.

go
inputFilePath := "***"
password := "***"
outputFileName := "***"

wordOptions := compdf.NewWordOptions()
wordOptions.EnableOCR = true
wordOptions.Languages = []compdf.OCRLanguage{compdf.OCRLangEnglish}
err := compdf.StartPDFToWord(inputFilePath, password, outputFileName, wordOptions, nil)

OCR Options

Different OCR options can be selected according to actual needs. Below are the currently supported OCR options.

  • OCRInvalidCharacter: Recognizes invalid or garbled characters in the PDF document through OCR, while normal characters are not processed by OCR.
  • OCRScanPage: Recognizes scanned pages in the PDF document through OCR, while editable pages are not processed by OCR.
  • OCRInvalidCharacterAndScanned: Recognizes both invalid characters and scanned pages in the PDF document through OCR.
  • OCRAll: Recognizes all pages and characters in the PDF document through OCR.

Preserve Page Background

When OCR is enabled, you can choose whether to enable the ContainPageBackgroundImage option. If this option is enabled, the original page background image of the PDF will be preserved. If it is disabled, the image result detected during page layout analysis will be retained.

Notice

  • The quality of the OCR result depends on the quality of the input image. If the input image has a low resolution, the OCR result quality will be affected. A good rule of thumb is that the more pixels in the character shapes, the better. If the character bounding box is smaller than 20x20 pixels, OCR quality will drop exponentially. The ideal image is a grayscale image with a resolution around 300 DPI.
  • When performing OCR, make sure the OCR language setting matches the language in the PDF document to achieve the best OCR conversion quality.
  • OCR functionality is currently not supported on operating systems lower than Windows 10.

Convert Images to Other Document Formats

The OCR function also supports converting input images into Word, Excel, PPT, HTML, CSV, RTF, TXT, JSON, and other formats. This sample demonstrates how to use the ComPDF OCR function to convert image files to a DOCX file.

go
inputFilePath := "***"
password := "***"
outputFileName := "***"

wordOptions := compdf.NewWordOptions()
wordOptions.EnableOCR = true
wordOptions.Languages = []compdf.OCRLanguage{compdf.OCRLangEnglish}
err := compdf.StartPDFToWord(inputFilePath, password, outputFileName, wordOptions, nil)

Sample

This Sample demonstrates how to use the ComPDF OCR function to convert a PDF to DOCX file.

go
inputFilePath := "***"
password := "***"
outputFileName := "***"

wordOptions := compdf.NewWordOptions()
wordOptions.EnableOCR = true
wordOptions.Languages = []compdf.OCRLanguage{compdf.OCRLangEnglish}
err := compdf.StartPDFToWord(inputFilePath, password, outputFileName, wordOptions, nil)