PDF to PNG Converter

Convert PDF to PNG using Java

In day to day routine, we receive documents in the form of PDF as they are quite popular over the internet for information sharing. But, we may have a requirement to prevent the users from copying the PDF file content, so one of the easiest approach is to save PDF as PNG. Now in order to perform the bulk conversion, a programmatic solution is the fastest and smartest approach. So in this article, we are going to discuss the details on how to convert PDF to PNG using Java Cloud SDK.

PDF to PNG Conversion API

Aspose.PDF Cloud SDK for Java enables the Java programmers to introduce the features related to PDF file creation, manipulation and conversion to variety of supported formats. So without using Adobe Acrobat or any other software, you can load the PDF and save into different formats. Now in order to develop PDF to PNG Converter, please add the following information in pom.xml of maven build type project.

<repositories> 
    <repository>
        <id>aspose-cloud</id>
        <name>artifact.aspose-cloud-releases</name>
        <url>http://artifact.aspose.cloud/repo</url>
    </repository>   
</repositories>

<dependencies>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-pdf-cloud</artifactId>
        <version>21.11.0</version>
    </dependency>
</dependencies>

The above details will add SDK references within Java projects and now the next important step is the creation of free account over Aspose Cloud. So please login using newly created account and lookup/create Client ID and Client Secret at Cloud Dashboard. These details are required in subsequent sections.

Convert PDF to PNG in Java

Its time to perform the PDF to PNG conversion using Java code snippet. Please note that the API call used in this section expects the source PDF file to be loaded from Cloud storage and in case its not available, we can easily upload it using one simple method.

  • First we need to create a PdfApi object while providing ClientID and Client secret as arguments
  • Secondly, load the input PDF file using File instance
  • Thirdly, upload the input PDF to cloud storage using uploadFile(…) method
  • Create Integer objects defining page number of PDF and the dimensions for resultant image
  • Finally call the getPageConvertToPng(…) method requiring input PDF, Page number and optional parameters defining resultant file dimensions. After conversion, the resultant image is returned as response stream

In case we have a requirement to save PDF as PNG and save the output to cloud storage, please follow the instructions specified below.

  • First we need to create a PdfApi object while providing ClientID and Client secret as arguments
  • Secondly, load the input PDF file using File instance
  • Thirdly, upload the input PDF to cloud storage using uploadFile(…) method
  • Now create string variables holding path details for resultant PNG, and Integer objects defining page number and dimensions for resultant PNG
  • Finally call the putPageConvertToPng(…) method requiring input PDF, path of resultant PNG, Page number and optional parameters defining resultant file dimensions
PDF to PNG online

Image1:- PDF to PNG Conversion Preview

The sample PDF file used in above example can be downloaded from PdfWithScreenAnnotations.pdf

Save PDF as PNG using cURL Commands

The cURL commands are also one of the convenient ways for accessing REST APIs through command line terminal. Now the pre-requisite for this approach is the generation of JWT access token (based on client credentials) while executing the following command.

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=bb959721-5780-4be6-be35-ff5c3a6aa4a2&client_secret=4d84d5f6584160cbd91dba1fe145db14" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Once we have the JWT token, we need to execute the following command to save PDF as PNG. Now in order to save the file locally, we are simply using -o argument and it saves the output on local drive.

curl -v -X GET "https://api.aspose.cloud/v3.0/pdf/input.pdf/pages/2/convert/png?width=800&height=1000" \
-H  "accept: multipart/form-data" \
-H  "authorization: Bearer <JWT Token>" \
-o resultant.png

Conclusion

This article has helped us in developing PDF to PNG converter using Java Cloud SDK. We have seen the examples to convert PDF to PNG using Java code snippets as well as we have learned the steps to save PDF as PNG using the cURL commands. We also provide the leverage to download complete source code of Java Cloud SDK from GitHub and you can modify it as per your requirements (its published under MIT license). In case of any issues, you may consider approaching us for a quick resolution via free product support forum.

We highly recommend visiting the following links to learn more about: