Converting a PDF document to an image format like JPEG or PNG can be useful for a variety of reasons, such as making it easier to share the content on social media, embedding it into a website, or including it in a presentation. Converting PDF to images can also avoid printing issues caused by the printers that do not support PDF format well enough. In this article, you will learn how to convert PDF to JPEG or PNG in Java using Spire.PDF for Java.
Install Spire.PDF for Java
First, 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 PDF to JPEG in Java
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 each page of a PDF document to a JPEG image file.
- Create a PdfDocument object.
- Load a PDF file using PdfDocument.loadFromFile() method.
- Iterate through each page of the PDF document.
- Convert a specific page into a BufferedImage object using PdfDocument.saveAsImage() method.
- Re-create a BufferedImage in RGB type with the same width and height as the converted image.
- Write the image data as a .jpg file using ImageIO.write() method.
- Java
import com.spire.pdf.PdfDocument; import com.spire.pdf.graphics.PdfImageType; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class ConvertPdfToJpeg { public static void main(String[] args) throws IOException { //Create a PdfDocument instance PdfDocument pdf = new PdfDocument(); //Load a sample PDF document pdf.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf"); //Loop through the pages for (int i = 0; i < pdf.getPages().getCount(); i++) { //Save the current page as a buffered image BufferedImage image = pdf.saveAsImage(i, PdfImageType.Bitmap, 300, 300); //Re-create a buffered image in RGB type 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-%d.jpg"), i)); ImageIO.write(newImg, "JPEG", file); } pdf.close(); } }
Convert PDF to PNG in Java
The steps to convert PDF to PNG using Spire.PDF for Java are as follows.
- Create a PdfDocument object.
- Load a PDF file using PdfDocument.loadFromFile() method.
- Iterate through each page of the PDF document.
- Convert a specific page into a BufferedImage object using PdfDocument.saveAsImage() method.
- Write the image data as a .png file using ImageIO.write() method.
- Java
import com.spire.pdf.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class ConvertPdfToPng { 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\\Input.pdf"); //Make the background of the generated PNG files transparent //doc.getConvertOptions().setPdfToImageOptions(0); //Loop through the pages for (int i = 0; i < doc.getPages().getCount(); i++) { //Save the current page as a buffered image BufferedImage 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(); } }
If the background of the PDF document is white and you want to make it transparent when converted to PNG, you can add following line of code before you save each page to a BufferedImage object.
- Java
doc.getConvertOptions().setPdfToImageOptions(0);
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.