Tutorials

How to Convert Image to Word Programmatically in C#

By ComPDFKit | Fri. 17 May. 2024
OCRConversion SDKC#

When it comes to handling files, there are times when we need to convert images into editable and reusable formats like Word documents. With the help of ComPDFKit Conversion SDK, developers can easily convert common image formats, such as jpg, png, and bmp, into .docx files using C#.

 

In this article, we will focus on using C# to convert image files to Word documents using the ComPDFKit Conversion SDK. We will cover the steps to integrate the SDK into your project and provide a detailed guide on how to convert images to Word documents programmatically.

 

 

Integrate ComPDFKit Conversion SDK to Convert Images to Word

 

1. Create a C# Project

 

We use Visual Studio 2017 to create a new Windows project.

 

- Fire up Visual Studio 2017, choose File -> New -> Project..., and then select Visual C# -> Windows Desktop -> Console App(.NET Framework) as shown in the following picture.

 

Create a New Windows Project in C# with ComPDFKit

 

- Choose the options for your new project as shown below. Please make sure to choose .NET Framework 4.6.1 as the programming framework.

 

Create a C# Project by ComPDFKit

 

- Place the project to the location as desired. Then, click OK.

 

2. Add ComPDFKit Conversion SDK Package

 

Before we dive into the conversion process, we need to make sure that the ComPDFKit Conversion SDK is properly integrated into our C# project. There are two ways to do this:

 

Integrate with a Nuget Package

 

- Open your project’s solution, and in the Solution Explorer, right-click on References and click on the menu item Manage NuGet Packages…. This will open the NuGet Package Manager for your solution.

 

Integrate with ComPDFKit Nuget Package

 

- Search for ComPDFKit.NetFramework, and you’ll find the package on nuget.org.

 

- On the right side, in the panel describing the package, click on the Install button to install the package.


install the package of ComPDFKit Conversion SDK

 

- Once that is complete, you’ll see a reference to the package in the Solution Explorer under References.


Reference of ComPDFKit Conversion SDK

 

Integrate within Visual Studio IDE Using Package Manager Console 

 

1. Copy all files in the "lib" folder to the project folder.

 

2. Add ComPDFKit Conversion SDK dynamic library to References. In order to use ComPDFKit Conversion SDK APIs in the project, you must first add a reference to it.

 

- In Solution Explorer, right-click the project and click Add -> Reference…

 

Integrate ComPDFKit Conversion SDK within Visual Studio IDE Using Package Manager Console

 

- In the Add Reference dialog, click the Browse tab, navigate to the project folder, select the "ComPDFKit_Conversion.dll" dynamic library, and then click OK.

 

Add

 

3. Add ComPDFKit Conversion SDK native library to the project. Add the "x64" and "x86" folder into the project. Please make sure to set the property "Copy to Output Directory" of "CPDFConverterNative.dll" and "opencv_world420.dll" to "Copy if newer". Otherwise, you should copy it to the same folder with the executable file manually before running the project.

 

Add the

 

4. Copy the "resource" folder to the project folder. Add the "resource" folder into the project. Please make sure to set the property "Copy to Output Directory" of the all files in the "resource" folder to "Copy if newer". Otherwise, you should copy it to the same folder with the executable file manually before running the project.

 

Add the ComPDFKit

 

Initialize the ComPDFKit Conversion SDK

 

It is important that you set the license key before using any ComPDFKit Conversion SDK classes.

 

string resPath = "***";
string libPath = "***";
string license = "***";
CPDFConverter.InitLibrary(libPath);
CPDFConverter.InitResource(resPath);
CPDFConverter.LicenseVerify(license);

 

 

Steps to Convert Images to Word in C#

 

Once the ComPDFKit Conversion SDK is integrated into your project, you can proceed with converting images to Word documents programmatically. To convert images to Word documents using C#, follow these steps:

 

- Set the input image file and the output Word file paths in your C# code.

string inputFilePath = "***";
string outputFolderPath = "***";
string outputFileName = "***";

 

- Input the image file.

CPDFConverterWord converter = CPDFConvertFactroy.CreateConverter(CPDFConvertType.CPDFConvertTypeWord,inputFilePath) as CPDFConverterWord;

 

- Configure the conversion options: In this step, you need to set the conversion parameters. Make sure to set the IsAllowOCR parameter to true to enable OCR (Optical Character Recognition) for extracting text from the image.

 

CPDFConvertWordOptions wordOptions = new CPDFConvertWordOptions();
wordOptions.IsAllowOCR = true;
wordOptions.OCRLanguage = ComDocumentAIOCR.Language.ENGLISH;
wordOptions.LayoutOpts = LayoutOptions.RetainPageLayout;

 

- Get the result of the image conversion: Once the conversion is complete, you can obtain the converted Word document as the output.

 

ConvertError error = ConvertError.ERR_UNKNOWN;
converter.Convert(outputFolderPath, ref outputFileName, wordOptions, pageArray, ref error, getPorgress);

 

 

Key Features of Converting Images to Word

 

Converting images to Word documents involves the crucial step of extracting and recognizing the content within the images. This is where Optical Character Recognition (OCR) comes into play. The ComPDFKit Conversion SDK provides robust OCR capabilities to accurately recognize and extract text from images.

 

Here are some key features of converting images to Word using the ComPDFKit Conversion SDK:

 

- Accurate OCR: The SDK employs advanced OCR algorithms to ensure accurate text recognition from images.

- Multiple Image Formats: The SDK supports a wide range of image formats, including jpg, png, and bmp, making it compatible with various image sources.

- Customizable Conversion Settings: You can customize the conversion settings according to your requirements, such as enabling or disabling OCR, specifying page ranges, and more.

- Seamless Integration: The ComPDFKit Conversion SDK seamlessly integrates with C# projects, allowing for smooth and efficient image-to-Word conversion.



Why Convert Images to Word?

 

There are several practical scenarios where converting images to Word documents can be beneficial:

 

- Editing and Reusing Content: By converting images to Word documents, you can easily edit and reuse the content within the images. This is particularly useful when you want to extract text or modify the layout of the content.

- Collaborative Work: Word documents are widely used for collaborative work. By converting images to Word, you can seamlessly incorporate image content into collaborative documents and enable easier collaboration among team members.

- Document Accessibility: Converting images to Word documents can improve document accessibility. Word documents offer features like screen reader compatibility, text resizing, and the ability to adjust formatting, making the content more accessible to users with disabilities.

 

In conclusion, converting images to Word documents programmatically using C# and the ComPDFKit Conversion SDK provides a convenient and efficient way to utilize image content in editable and reusable formats. Whether you need to extract text from images or incorporate image content into collaborative documents, the ComPDFKit Conversion SDK offers powerful features to meet your requirements.

 

For more information about converting files to various formats, such as PDF to Word, Excel, PowerPoint, HTML, RTF, CSV, TXT, JSON, and more, refer to the free online tools and explore the wide range of conversion capabilities offered by the ComPDFKit Conversion SDK.

 

Read More

How to Extract Text from PDFs in C# (Code Example Tutorial)

Extract Text From PDF in C# Using iTextSharp VS ComPDFKit

How to Build a Windows PDF Viewer or Editor in C#