How to Convert PDF to Images in Java

PDF files and image files are two completely different formats, but sometimes there is a need to convert the two formats to each other. How do you solve the problem of converting PDF files to image files at work? Is the method you use simple and convenient? If it is very troublesome, you might as well learn how to share this article. This article will introduce how to quickly and efficiently convert PDF to image format through a Java application.

  • Convert a Whole PDF Document to Multiple Images
  • Convert a Particular PDF Page to an Image

Programming Environment

Method 1:

Introduced manually. Download Free Spire.PDF for Java locally, unzip it, and find the Spire.PDF.jar file in the lib folder. Open the following interface in IDEA, and import the jar file in the local path into the Java program:

If you use Maven, you can easily import the JAR file in your application by adding the following code to your project’s pom.xml file.

<repositories>

    <repository>

        <id>com.e-iceblue</id>

        <name>e-iceblue</name>

        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>

    </repository>

</repositories>

<dependencies>

    <dependency>

        <groupId>e-iceblue</groupId>

        <artifactId>spire.pdf.free</artifactId>

        <version>5.1.0</version>

    </dependency>

</dependencies>

Convert a Whole PDF Document to Multiple Images

The following are steps to convert a whole PDF document to multiple images.

  • Create a PdfDocument instance.
  • Load a PDF sample document using PdfDocument.loadFromFile() method.
  • Loop through all pages of the document and set the image Dpi when converting them to images using PdfDocument.saveAsImage(int pageIndex, PdfImageType type, int dpiX, int dpiY) method.
  • ave images to a specific folder as .png files.

Full Code

Java

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.graphics.PdfImageType;
import javax.imageio.ImageIO;

public class WholePDFToImages {
public static void main(String[] args) throws IOException {
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();

//Load a PDF sample document
pdf.loadFromFile("Cardigan Welsh Corgi.pdf");

//Loop through every page
for (int i = 0; i < pdf.getPages().getCount(); i++) {
//Convert all pages to images and set the image Dpi
BufferedImage image = pdf.saveAsImage(i, PdfImageType.Bitmap,500,500);
//Save images to a specific folder as a .png files
File file = new File("D:\\IntelliJ IDE\\free PDF\\output\\PDFToImages" + "/" + String.format(("ToImage-img-%d.png"), i));
ImageIO.write(image, "PNG", file);
}
pdf.close();
}
}

Effective Shot


Convert a Particular PDF Page to an Image

The following steps show you how to convert a particular PDF page to an image.

  • Create a PdfDocument instance.
  • Load a PDF sample document using PdfDocument.loadFromFile() method.
  • Convert a specific page to an image and set the image Dpi using PdfDocument.saveAsImage(int pageIndex, PdfImageType type, int dpiX, int dpiY) method.
  • Save the image to another file as a .png format.

Full Code

Java

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.graphics.PdfImageType;
import javax.imageio.ImageIO;

public class ParticularPDFToImage {
public static void main(String[] args) throws IOException {
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();

//Load a PDF sample document
pdf.loadFromFile("Cardigan Welsh Corgi.pdf");

//Convert the second page to an image and set the image Dpi
BufferedImage image= pdf.saveAsImage(1, PdfImageType.Bitmap,500,500);

//Save the image to another file as a .png format
ImageIO.write(image, "PNG", new File("ToPNG.png"));
}
}

Effective Shot

Conclusion:

In this article, you have learned how to convert PDF to Word in Java. Not only that, we also have other functions, such as, Java: Convert PDF to PDF/A, Java: Convert PDF to Excel and so on. Apart from that, if you'd like to learn more, you can visit this link to explore more about for Spire.PDF for Java.

Comments

Popular posts from this blog

Java: How to encrypt or decrypt PDF documents?

Replace the existing image on the PDF file in C#/VB.NET