Tuesday, 29 March 2022 08:37

Java: Convert PDF to Images

Although PDF documents are widely supported across different devices and platforms, images may be more suitable for certain tasks since they can be easily added to videos or other documents, especially when you only wish to display one PDF page. This article will demonstrate how to programmatically convert PDF to images from the following two aspects using Spire.PDF for Java.

Install Spire.PDF for Java

First of all, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. 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</artifactId>
        <version>10.10.7</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.
  • Save images to a specific folder as .png files.
  • 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("sample.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("C:\\Users\\Administrator\\Desktop\\PDFToImages" + "/" + String.format(("ToImage-img-%d.png"), i));
            ImageIO.write(image, "PNG", file);
        }
        pdf.close();
    }
}

Java: Convert PDF to Images

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.
  • 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("sample.pdf");

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

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

Java: Convert PDF to Images

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Conversion
Page 3 of 3