With ComPDFKit API library for Java, you not only have the capability to convert PDFs to images but also to and from other formats such as Word, Excel, PPT, images, HTML, JSON, and more. Try to use our online for free. Beyond just PDF conversion, our API libraries empower you to:
• Merge, split, insert, extract, and delete specific PDF pages
• OCR, watermark, or compress PDFs
• Compare documents (including content comparison and overlay comparison)
• Extract the images from the selected pages from PDFs
• Analyze and recognize text, images, paragraphs, headings, tables, etc. from PDFs
• Extract unstructured data from PDFs and save as structured data
To add converting PDF to images feature in Java projects in a few minutes, you can get started with the following steps.
Creating a Free API Account on ComPDFKit
In the first place, you could to get the credential of our API Java library. This free trial allows you to process 1000 documents per month, and you'll be able to access all our PDF API tools. After successfully log in your account, you'll enter the page below, providing an overview of your plan details.
Setup Java Environment
1. Install if you haven't already.
2. Run javac -version to verify your install.
3. Verify the JDK bin folder is included in the PATH variable (method varies by OS).
4. Install using your preferred tool if you haven't already.
5. For this tutorial, you'll use IntelliJ IDEA as your primary code editor. To get started, create a new project called pdf_to_jpg. You can choose any location, but make sure to select Java as the language, Maven as the build system.
- Creating a Sample Project
- Install two libraries:
- OkHttp — This library makes API requests.
- JSON — This library will parse the JSON payload.
Open the pom.xml file and add the following dependencies to your project:
<dependencies>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.2</version>
</dependency>
</dependencies>
6. Open the main.java file and paste the code as in step2:
Use Java API Library to Convert PDF to Images
Step 1. Obtaining the API Key for Authentication
After you've verified your email, you can get your API key from the dashboard. In the menu on the left, click API Keys. You'll see the following page, which is an overview of your keys:
Now You need to replace public_key and secret_key with accessToken in the publicKey and secretKey authentication return values you get from the console.
import java.io.*;
import okhttp3.*;
public class main {
public static void main(String []args) throws IOException{
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("text/plain");
// Building request parameters
RequestBody body = RequestBody.create(mediaType, "{\n \"publicKey\": \"{{public_key}}\",\n \"secretKey\": \"{{secret_key}}\"\n}");
Request request = new Request.Builder()
.url("https://api-server.compdf.com/server/v1/oauth/token")
.method("POST", body)
.build();
// Request results
Response response = client.newCall(request).execute();
}
}
Step 2: Run the Project to Convert PDF to Images
1. Creating Task for JPG and PNG
You need to replace the accessToken which was obtained from the previous step, and replace the language type you want to display the error information. After replacing them, you will get the taskId in the response data.
- PDF to JPG
import java.io.*;
import okhttp3.*;
public class main {
public static void main(String []args) throws IOException{
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("text/plain");
// Building request parameters
RequestBody body = RequestBody.create(mediaType, "");
Request request = new Request.Builder()
//pdf to jpg
.url("https://api-server.compdf.com/server/v1/task/pdf/jpg?language={{language}}")
.method("GET", body)
.addHeader("Authorization", "Bearer {{accessToken}}")
.build();
// Request results
Response response = client.newCall(request).execute();
}
}
- PDF to PNG
import java.io.*;
import okhttp3.*;
public class main {
public static void main(String []args) throws IOException{
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("text/plain");
// Building request parameters
RequestBody body = RequestBody.create(mediaType, "");
Request request = new Request.Builder()
//pdf to png
.url("https://api-server.compdf.com/server/v1/task/pdf/png?language={{language}}")
.method("GET", body)
.addHeader("Authorization", "Bearer {{accessToken}}")
.build();
// Request results
Response response = client.newCall(request).execute();
}
}
2. Uploading Files
Replace the file you want to convert, the taskId obtained in the previous step, the language type you want to display the error information, and the accessToken obtained in the first step.
Parameters:
pages:Value range: 1 to the maximum number of pages in the document (default: all pages).
imgDpi:Value range 72-1500 (default 300 , JPG support only).
import java.io.*;
import okhttp3.*;
public class main {
public static void main(String []args) throws IOException{
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("text/plain");
// Building request parameters
RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("file","{{file}}",
RequestBody.create(MediaType.parse("application/octet-stream"),
new File("")))
.addFormDataPart("taskId","{{taskId}}")
.addFormDataPart("language","{{language}}")
// PNG Param
//.addFormDataPart("parameter","{ \"pages\":[1]}")
// JPG Param
//.addFormDataPart("parameter","{ \"imgDpi\": \"300\",\"pages\":[1]}")
.addFormDataPart("password","")
.build();
Request request = new Request.Builder()
.url("https://api-server.compdf.com/server/v1/file/upload")
.method("POST", body)
.addHeader("Authorization", "Bearer {{accessToken}}")
.build();
// Request results
Response response = client.newCall(request).execute();
}
}
3. Processing Files
Replace the taskId you obtained from the Create task, and the accessToken obtained in the first step, and replace the language type you want to display the error information.
import java.io.*;
import okhttp3.*;
public class main {
public static void main(String []args) throws IOException{
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("text/plain");
RequestBody body = RequestBody.create(mediaType, "");
// Building request parameters
Request request = new Request.Builder()
.url("https://api-server.compdf.com/server/v1/execute/start?taskId={{taskId}}&language={{language}}")
.method("GET", body)
.addHeader("Authorization", "Bearer {{accessToken}}")
.build();
// Request results
Response response = client.newCall(request).execute();
}
}
4. Getting Task Information
Replace taskId with the taskId you obtained from the step "Create the task", access_token replaced by access_token obtained in the first step.
import java.io.*;
import okhttp3.*;
public class main {
public static void main(String []args) throws IOException{
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("text/plain");
RequestBody body = RequestBody.create(mediaType, "");
// Building request parameters
Request request = new Request.Builder()
.url("https://api-server.compdf.com/server/v1/task/taskInfo?taskId={{taskId}}")
.method("GET", body)
.addHeader("Authorization", "Bearer {{accessToken}}")
.build();
// Request results
Response response = client.newCall(request).execute();
}
}
Final Words
You can now seamlessly integrate the PDF-to-image conversion capability into your current applications or systems through our PDF to Image API for Java. If you're interested in delving deeper into utilizing ComPDFKit's API library, our comprehensive documentation offers detailed tutorials on various functionalities such as , editing PDF pages, and more. a free ComPDFKit API account and explore our documentation for comprehensive insights into maximizing the potential of our API library.
Resources and Support
• For detailed information, please visit .
• Stay updated with the latest improvements through our .
• For technical assistance, please reach out to our .
• For troubleshooting and , please on our GitHub.
• To get more details and an accurate quote, please .
• Related articles:
- Convert PDF to Excel Using Java API Library