PDF to JPG

The Portable Document Format (PDF) is one of the popular file formats for information and data sharing. One of its exciting features is the prevention of document formating when viewing the document on any platform. But, we may stumble upon a requirement to convert these files to raster image formats such as JPG, PNG, BMP, etc. So in this article, we are going to discuss the details on how to convert PDF to JPG online using REST API.

PDF processing API

Aspose.PDF Cloud is our award-winning Cloud API offering the capabilities to create, edit and convert PDF files on the fly. No additional software download or installation is required. Also, please note that the API is developed as per REST architecture, so it can be used on any platform and any application type including Desktop, Mobile, Web, Hybrid, etc. For further information regarding API capabilities, we recommend visiting Aspose.PDF Cloud Features.

Installation & Licensing

In this article, our focus is towards PDF files processing using C# .NET and for that purpose, we are going to use Aspose.PDF Cloud SDK for .NET which is a wrapper around Aspose.PDF Cloud. Now the first step is to download the SDK either from NuGet or GitHub. For current scenario, we are going to install it from the NuGet library by executing the following command in the NuGet package manager:

Install-Package Aspose.Pdf-Cloud -Version 20.12.0

If we skip the version details, the latest version will be fetched automatically.

Another approach is to use the NuGet package manager. Expand project tree in Solution Explorer, right-click Packages folder and select Manage NuGet Packages… option from the context menu.

NuGet packages

Image 1:- Manage NuGet packages option.

Now search Aspose.PDF Cloud in the search field, enable the checkbox besides the package name and click Add Package button.

Aspose.PDF Cloud NuGet

Image 2:- Aspose.PDF Cloud in NuGet packages.

Notice that Aspose.Pdf-Cloud.dll appears under the Packages folder.

Image 3:- Aspose.Pdf-cloud.dll under solution explorer.

Image 3:- Aspose.Pdf-cloud.dll under solution explorer.

Licensing is a very important aspect for any application and here at Aspose, we have precisely designed the licensing mechanism. The pricing model is pay as you go and very much competitive because you can perform document processing at as low as $0.005 / API Call. You may check the pricing page for further details. However, before you opt for licensing, you may 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 4:- Client credentials on the dashboard.

PDF to JPG in C#

Please follow the steps given below to perform the conversion.

  • The first step is to create string variables holding Client ID and Client Secret details.
  • Secondly, create an instance of PdfApi while passing Client ID and Client Secret as arguments
  • Thirdly, create string variables defining input PDF, page number to be converted and output file name
  • Finally, call the PutPageConvertToJpeg(…) method of PdfApi class to perform the conversion
PDF to JPG output

Image 5:- PDF to JPG conversion preview.

The sample files used in above example can be downloaded from following links

Convert PDF to JPG using the cURL command

As 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=b1a1b925-cbd0-40c3-b7d5-075c93601243&client_secret=343ebf767f3f53537a45ced31d6be34f" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Now execute the following cURL command to load PDF document available in Cloud storage, perform conversion to JPG and return resulting file in response. However, we have saved the output to local system drive using -o argument. Also, please note that we have set the dimensions for resultant file using optional Height and Width parameters.

curl -X GET "https://api.aspose.cloud/v3.0/pdf/converted.pdf/pages/1/convert/jpeg?width=800&height=800" \
-H  "accept: multipart/form-data" \
-H  "authorization: Bearer <JWT Token>" \
-o newConverted.jpg

Conclusion

In this article, we have learned about a very convenient and quick technique to convert PDF pages to JPG image format. The whole conversion is performed using fewer code lines, without utilizaing Adobe Acrobat or any other application. We also have discussed the steps regarding PDF to JPG conversion using the cURL command. Furthermore, please note that Aspose.PDF Cloud SDK for .NET is distributed under an MIT license and its complete source code is available for download over GitHub.

You may consider visiting following links to learn more about