DEV Community

AnnaGraz
AnnaGraz

Posted on

Java: Convert PDF to Images in JPG, PNG, TIFF and SVG

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, 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 PDF to JPG in Java
Convert PDF to SVG
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. 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>9.6.2</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

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.

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"));
    }
}
Enter fullscreen mode Exit fullscreen mode

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.
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();
    }
}
Enter fullscreen mode Exit fullscreen mode

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.

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

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.

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");
    }
}
Enter fullscreen mode Exit fullscreen mode

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, PDF to HTML and etc. Wish it helps.

Top comments (0)