DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Alexis
Alexis

Posted on

Java - How to Convert PDF to Images (PNG, JPG, SVG, TIFF)

PDF files are ideal for file sharing, and carry the assurance that the contents within the document will display correctly across devices without being altered. But, there are times where it’s favorable to convert a PDF document into individual image files. For instance, converting PDF to a JPG file allows you to decrease the overall size of the document. This means that it will be easier for you to transfer files to another person, upload them to a website, etc.

In this article, I am going to show you how to convert PDF to PNG, JPG, SVG, and TIFF files in Java using Spire.PDF for Java library.

Add Spire.Pdf.jar as dependency

If you are working on a maven project, you can include the dependency in pom.xml file using this:

<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>8.11.0</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

If you are not using maven, then you can find the required jar files from the zip file available in this location. Include all the jar files into the application lib folder to run the sample code given in this tutorial.

Convert PDF to PNG in Java

A PNG file is an image saved in the Portable Network Graphic (PNG) format, commonly used to store web graphics, digital photographs, and images with transparent backgrounds. The following are the steps to convert a PDF document to multiple PNG image files using Spire.PDF for Java.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.loadFromFile() method.
  • Make background of the generated image transparent by passing 0 to PdfDocument.getConvertOptions().setPdfToImageOptions() method.
  • Traverse the pages in the document, and save a specific page as buffered image using PdfDocument.saveAsImage(pageIndex) method.
  • Write the image data as a PNG file using ImageIO.write() method.
import com.spire.pdf.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class ConvertPdfToTransparentPng {

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

        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        //Load a sample PDF document
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");

        //Make background transparent
        doc.getConvertOptions().setPdfToImageOptions(0);

        //Declare a BufferedImage variable
        BufferedImage image;

        //Loop through the pages
        for (int i = 0; i < doc.getPages().getCount(); i++) {

            //Save the current page as a buffered image
            image = doc.saveAsImage(i);

            //Write the image data as a .png file
            File file = new File("C:\\Users\\Administrator\\Desktop\\Output\\" + String.format("ToImage-%d.png", i));
            ImageIO.write(image, "png", file);
        }
        doc.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

PdfToPng

Convert PDF to JPG in Java

A JPG file is a raster image saved in the JPEG format, commonly used to store digital photographs and graphics created by image-editing software. The following are the steps to convert a PDF document to individual JPG files using Spire.PDF for Java.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.loadFromFile() method.
  • Traverse the pages in the document, and save a specific page as buffered image using PdfDocument.saveAsImage(pageIndex) method.
  • Write the image data as a JPG file using ImageIO.write() method.
import com.spire.pdf.PdfDocument;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ConvertPdfToJpg {

    public static void main(String[] args) throws IOException {

        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        //Load a sample PDF document
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");

        //Declare a BufferedImage variable
        BufferedImage image;

        //Loop through the pages
        for (int i = 0; i < doc.getPages().getCount(); i++) {

            //Save the current page as a buffered image
            image = doc.saveAsImage(i);

            //Re-create a buffered image
            BufferedImage newImg = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
            newImg.getGraphics().drawImage(image, 0, 0, null);

            //Write the image data as a .jpg file
            File file = new File("C:\\Users\\Administrator\\Desktop\\Output\\" + String.format("ToImage-img-%d.jpg", i));
            ImageIO.write(newImg, "JPG", file);
        }
        doc.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

PdfToJpg

Convert PDF to SVG in Java

An SVG is a Scalable Vector Graphic. This means that the graphic can be scaled to any size without losing quality. The following are the steps to convert a PDF document to a single or several separate SVG files using Spire.PDF for Java.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.loadFromFile() method.
  • Save each page as an individual SVG file using PdfDocument.saveToFile() method.
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;

public class ConvertPdfToSvg {

    public static void main(String[] args) {

        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        //Load a sample PDF document
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");

        //If you want the PDF document to be converted as a single SVG file, pass true to setOutputToOneSvg() method
        //doc.getConvertOptions().setOutputToOneSvg(true);

        //Save each page as an individual SVG file
        doc.saveToFile("C:\\Users\\Administrator\\Desktop\\Output\\ToSVG.svg", FileFormat.SVG);
        doc.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

PdfToSvg

Convert PDF to TIFF in Java

TIFF is a file type that it's best to use for high-quality images that need to be edited. TIFF files are large, so they’re not ideal for sharing online. Conversion from TIFF to PDF only needs 3 lines of code, as shown below.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.loadFromFile() method.
  • Save the PDF file to TIFF using PdfDocument.saveToTiff() method.
import com.spire.pdf.PdfDocument;

public class ConvertPdfToTiff {

    public static void main(String[] args) {

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

        //Load a PDF document
        pdf.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");

        //Convert the entire PDF document to TIFF
        pdf.saveToTiff("C:\\Users\\Administrator\\Desktop\\Output\\PDFtoTiff.tiff");
    }
}
Enter fullscreen mode Exit fullscreen mode

PdfToTiff

Top comments (0)

An Animated Guide to Node.js Event Lop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.