Java: Convert PDF to Images in JPG, PNG, TIFF and SVG
Tags: image, pdf, java, free
Published: 2023-07-06 08:31:39

Developers often need to convert PDF pages to images since images are difficult to edit and easy to view. With the help of[ Spire.PDF for java](https://www.e-iceblue.com/Introduce/pdf-for-java.html), we can easily convert PDF file to image with high quality in Java applications. In this article, we will explain the solutions of converting PDF document to bitmap images and vector images from the following four parts:

[Convert PDF to PNG](#convert-a-specific-pdf-page-to-png)
[Convert PDF to JPG in Java](#convert-all-pdf-pages-to-jpeg)
[Convert PDF to SVG](#convert-pdf-to-svg)
[Convert PDF to TIFF](#convert-pdf-to-tiff)

###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 [PDF API](https://www.e-iceblue.com/Download/pdf-for-java.html). 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.

```java
<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>9.6.2</version>
    </dependency>
</dependencies>
```

###Convert a specific PDF page to PNG

The PdfDocument.saveAsImage() method provided by Spire.PDF for Java enables the conversion of a particular page from a PDF document into a BufferedImage object, which can be saved as a file in .jpg or .png format. The following are the steps to convert a specific page of a PDF document to a PNG image file.

```java
import java.awt.image.BufferedImage;
import java.io.File;
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 Exception {

        //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"));
    }
}
```
###Convert all PDF pages to JPEG
Here are the steps to convert PDF to JPEG.

- Create a PdfDocument object.
- Load a PDF file using PdfDocument.loadFromFile() method.
- Iterate through each page of the PDF document.
- Convert all pages into a BufferedImage object using PdfDocument.saveAsImage() method.
- Write the image data as a .jpeg file using ImageIO.write() method.

```java
import java.awt.image.BufferedImage;
import java.io.File;
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 Exception {

        //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("output" + "/" + String.format(("ToImage-img-%d.jpg"), i));
            ImageIO.write(image, "JPEG", file);
        }
        pdf.close();
    }
}
```
###Convert PDF to SVG
SVG, short for scalable vector graphics, is a vector image format based on XML for two-dimensional graphics. Vector image files, like SVG and PDF files, are very similar. They can display text, images, and other elements in the same appearance and keep the definition no matter how you zoom them. Spire.PDF offers **PdfDocument.saveToFile()** method to convert PDF to SVG.

```java
import com.spire.pdf.*;

public class PDFtoSVG{
    public static void main(String[] args) throws Exception {

        //Create an object of Document class
        PdfDocument pdf = new PdfDocument();

        //Load a PDF document from disk
        pdf.loadFromFile("Sample.pdf");

        //Convert the document to SVG and Save it
        pdf.saveToFile("output/PDFtoSVG.svg", FileFormat.SVG);
    }
}
```

###Convert PDF to TIFF
TIFF shot for Tagged Image File Format, is a relatively flexible image format which has the advantages of not requiring specific hardware, as well as being portable. Spire.PDF offers **PdfDocument.saveToTiff(String tiffFilename)** method to save the PDF page as a TIFF image.
```java
import com.spire.pdf.PdfDocument;


public class PDFtoTIFF{
    public static void main(String[] args) throws Exception {

        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();

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

        //Save all pages of the document to Tiff
        pdf.saveToTiff("output/PDFtoTiff.tiff");
    }
}
```
###Conclusion

In this article, you have learned how to convert PDF pages to image programmatically in Java. Moreover, Spire.PDF also support to convert[ HTML to PDF](https://www.e-iceblue.com/Tutorials/Java/Spire.PDF-for-Java/Program-Guide/Conversion/Java-convert-HTML-to-PDF.html), [PDF to HTML](https://www.e-iceblue.com/Tutorials/Java/Spire.PDF-for-Java/Program-Guide/Conversion/Convert-PDF-to-HTML-in-Java.html) and etc. Wish it helps.