Images and scanned documents may contain text information that you may need to process further. You may have taken a picture of a text document using a smartphone and turned it into an editable document. To this end, performing OCR on images can prove helpful. Using OCR, images can be converted into searchable and editable Word documents. To do this, this article will show you how to use C++ to convert images into Word documents.

  • Use C++ to convert images into Word documents
  • Convert slanted images into Word documents using C++

Aspose.OCR is an optical character recognition API that extracts text from images. The API also enables you to convert images into Word documents and text files. If you haven’t used Aspose.OCR you can download the latest version of the test.

Use C++ to convert images into Word documents

Often you might need to search for or edit the text in the image, but you cannot do so. By performing OCR on such images and converting them into Word documents, you can search and edit text as needed. To do this, follow these steps.

  • Prepare buffers for results.
  • Set the recognitionSettings. save_format structure member to file_format::docx.
  • OCR operation on the picture, Save the Word file using the asposeocr_page_save(const char * image_PATH, const char * save_path, RecognitionSettings Settings) method.

The following example code shows how to convert an image to a Word document using C++ :

// Source file path
std::string image_path = "SourceDirectory\\sample.png";

// Prepare buffer for result (in symbols, len_byte = len * sizeof(wchar_t))
const size_t len = 4096;
wchar_t buffer[len] = { 0 };

// Set the recognition settings
RecognitionSettings settings;
settings.save_format = file_format::docx;

// Perform the OCR operation and save the output file.
asposeocr_page_save(image_path.c_str(), "OutputDirectory\\ImageToDocx.Docx", settings);
Copy the code

Convert slanted images into Word documents using C++

Images and scanned documents are sometimes slant. Using the Aspose.OCR for C++ API, you can perform OCR on slanted images. Here are the steps to turn a slanted image into a Word document:

  • Prepare buffers for results.
  • Calculate the tilt Angle of the image using the asposeocr_get_skew(const char * image_path) method.
  • Set the recognitionSettings. save_format structure member to file_format::docx.
  • Use recognitionSettings. skew structure member to specify the tilt Angle.
  • OCR operation on the picture, Save the Word file using the asposeocr_page_save(const char * image_PATH, const char * save_path, RecognitionSettings Settings) method.

The following example code shows how to convert a slanted image into a Word document using C++ :

// Source file path
std::string image_path = "SourceDirectory\\skewSample.png";

// Prepare buffer for result (in symbols, len_byte = len * sizeof(wchar_t))
const size_t len = 4096;
wchar_t buffer[len] = { 0 };

// Calculate skew angle
std::double_t angle = asposeocr_get_skew(image_path.c_str());

// Set the recognition settings
RecognitionSettings settings;
settings.save_format = file_format::docx;
settings.skew = angle;

// Perform the OCR operation and save the output file.
asposeocr_page_save(image_path.c_str(), "OutputDirectory\\SkewedImageToDocx.docx", settings);
Copy the code

If you have any questions or requirements, please feel free to join the Aspose Technology Exchange Group (761297826), we are happy to provide you with inquiries and consultation.