Word to JPG – Convert Word to JPG in C#

Word to JPG

In this article, we are going to discuss the conversion of Word documents to JPG format. The MS Word files including DOC, DOCX, DOCM, DOTX, ODT, OTT, etc are quite popular file formats for information storage and sharing in organizations, universities, and other institutes. In addition to creating simple documents for business correspondence, Word format helps you greatly in creating and designing business cards, brochures, new letters, and many more items. However, in order to view these files, you need to use relevant software installed which costs time and money. Furthermore, you may have a requirement to restrict users from copying and editing the Word files, so the conversion to raster images such as JPG format is a viable solution.

Another benefit of converting word files to JPG is compressed output size as JPG format greatly reduces the file size.

Word processing SDK

Aspose.Words Cloud is our award-winning REST API providing capabilities to create, edit, manipulate and render MS Word, OpenOffice, and other popular formats. Furthermore, in order to facilitate our customers, we have created specific programming SDKs so that all the features become available within the language of your choice. In this article, we are going to discuss Word document processing within the .NET application using Aspose.Words Cloud SDK for .NET.

Installation

The SDK is available for download from NuGet and GitHub. So if wish to install Install Aspose.Words Cloud SDK for .NET via NuGet, please execute the following command in terminal:

nuget install Aspose.Words-Cloud

Or execute the following command in NuGet Package Manager:

PM> Install-Package Aspose.Words-Cloud

Another approach is the installation directly within Visual Studio:

  1. Open the Solution Explorer.
  2. Expand the project and Right-click Packages folder within your solution.
  3. Select Manage NuGet Packages… option
  4. Click on the Browse tab and search for “Aspose.Words-Cloud“.
  5. Click on the Aspose.Words-Cloud package, select the appropriate version in the right-tab and click Add Package button.
Manage NuGet packages
Image 1:- Manage NuGet packages menu option.
Aspose.Words-Cloud
Image 2:- Aspose.Words Cloud NuGet package.

Account subscription

Free account subscription. No credit card or payment details are required. Furthermore, you are entitled to perform up to 150 free document processing calls under a free subscription. The reason we ask for sign-up is that only authorized persons shall have an access to our system. So, please create a free account by visiting Aspose.Cloud dashboard. If you have GitHub or Google account, simply Sign Up. Otherwise, click on the Create a new Account button and provide the required information. Now login to the dashboard using credentials and expand the Applications section from the dashboard and scroll down towards the Client Credentials section to see Client ID and Client Secret details.

Client Credentials
Image 3:- Client credentials on the dashboard.

Word to JPG in C#

In this section, we are going to discuss the steps and their related details for Word to JPG conversion using C# .NET code:

  • First of all, we need to create an object of Configuration class
  • Secondly, initialize WordsApi instance by passing Configuration object as an argument
  • Thirdly, read the content of Word file and upload to cloud storage using UploadFile(..) method of WordsApi class
  • Now create an instance of GetDocumentWithFormatRequest and pass the name of input Word file, output format and resultant file name as arguments
  • Finally, call the GetDocumentWithFormat(…) method of WordsApi to perform the converison. Please note the output JPG is stored in cloud storage
Word to PDF preview
Image 4:- Word to JPG conversion preview.

DOCX to JPEG in C#

Let’s discuss the scenario where you want to perform DOCX file conversion to JPEG format without first uploading the source word file to Cloud storage. Please follow the steps specified below to accomplish this requirement.

  • First of all, we need to create an object of Configuration class
  • Secondly, initialize WordsApi instance by passing Configuration object as an argument
  • Now create an instance of ConvertDocumentRequest which takes input DOCX path, output format and resultant file name as arguemnts
  • Finally, call the ConvertDocument(..) method to initialize the conversion process. Resultant file is stored in Cloud storage

DOC to JPG using cURL command

Since the API is developed according to REST architecture, we can access it via cURL commands. But, since its only accessible to authorized persons, so we need to first generate a JSON Web Token (JWT) based on ClientID and ClientSecret details retrieved from Aspose.Cloud dashboard. Please execute the following command in the terminal to generate the JWT token.

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=4ccf1790-accc-41e9-8d18-a78dbb2ed1aa&client_secret=caac6e3d4a4724b2feb53f4e460eade3" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Once the JWT token has been generated, please execute the following command to convert DOC file already available in cloud storage to JPG format.

curl -X GET "https://api.aspose.cloud/v4.0/words/sample1.doc?format=jpg&outPath=Converted.jpg&fontsLocation=fonts" \
-H  "accept: application/octet-stream" \
-H  "Authorization: Bearer <JWT Token>"

The sample files used in above examples can be download from sample1.docx and converted.jpg.

Conslusion

In this article, we have discussed the details on how to convert Word document to JPG format with few code lines. We have also explored the option to render DOC file to JPG format using the cURL command. Furthermore, please note that Aspose.Words Cloud SDK for .NET is distributed under an MIT license and its complete source code is available for download over GitHub.

In case you encounter any issue while using the API, please feel free to contact via Free support forum.

Related articles

We also recommend visiting following link to learn more about