Pdf to jpeg java

Java PDF to Image file

Converting PDF pages/document to image files such as JPEG image, PNG or TIFF has many applications in software industry. Sometimes, you need an image of specific pages of PDF file to use somewhere but your only option is image screenshot of the page. Imagine you are working on your Java project that has a functionality to load and convert PDF page to image files and use them for other purposes. Using traditional Java code, the conversion is near to impossible. For that purpose, we will use IronPDF for Java.

How to Convert PDF to Image in Java

  1. Install Java library to convert PDF to various image formats
  2. Utilize toBufferedImages method to convert PDF to image
  3. Convert URL to image by first get PDF with renderUrlAsPdf method
  4. Use step 2 to convert PDF to desired image format
  5. Use write method to export each image

2. IronPDF for Java

IronPDF for Java is a package that allows you to create, prepare, and manage PDF files. It is popular among developers because of its PDF document generation component, which allows them to read PDF, generate/create, and modify PDF files without the need for Adobe Acrobat. IronPDF for Java supports custom headers/footers, signatures, attachments, passwords, and security mechanisms. One of the improved performance features is full multithreading and async support. IronPDF for Java works with Maven based projects.

Читайте также:  Beautiful soup python 3 pip

Below we will discuss how to convert PDF pages to image formats like JPEG image, JPG or PNG images using Java.

3. Prerequisites

Before we get started, there are a few points that must be present to carry out this conversion.

  1. Java should be installed in the system and its path should be set in Environment Variables. Please refer to this link to install Java if you don’t have before.
  2. A good Java IDE should be installed, like Eclipse or IntelliJ. To download Eclipse, please visit this link and to download IntelliJ, please click on this link.
  3. Maven should be integrated with the IDE before starting with conversion. For the tutorial of installing Maven and integrating it to the environment, visit the following link[link].

4. IronPDF for Java Installation

Once all the prerequisites are fulfilled, installing IronPDF for Java is quite simple and easy for even new Java developers.

To use IronPDF for Java, first you need an IDE. For this article, we will use JetBrains IntelliJ IDEA to install the required dependencies and run examples.

First, open JetBrains IntelliJ IDEA and create new Maven project.

Java PDF to Image - Figure 1: Create new Maven Project

Create a new Maven Project

A new window will appear. Enter the name of project and click on Finish.

Java PDF to Image - Figure 2: New Project Name

After you click finish, a new project will open and by default pom.xml is opened in the project which is good because we need to add Maven dependencies of IronPDF for Java.

Java PDF to Image - Figure 3: New Project

Add the following dependencies in pom.xml file or you can download API’s JAR file from the following link.

 
com.ironsoftware
com.ironsoftware
2023.7.2

Once you place the dependencies in the pom.xml file, a small icon will appear in the right top corner of the window.

Java PDF to Image - Figure 4: Maven Dependencies

Click on this icon to install the Maven dependencies. This will only take a few minutes depending on your internet connection.

5. Convert PDF File to Images using IronPDF for Java

Using IronPDF for Java, PDF to image conversion such as JPEG is just a few lines of code. It converts input PDF document into the output stream of images. It the toBufferedImages method that returns a list containing a collection of BufferedImage objects, and it is arranged in ascending order corresponding with page numbers.

Using IronPDF for Java you cannot only generate images from PDF documents, but you can also create images from URLs and HTML directly.

5.1. Converting PDF Document to Images

In this following example, we will convert the entire PDF document into images. To get started, just write the following code and run the program.

import com.ironsoftware.ironpdf.PdfDocument; import com.ironsoftware.ironpdf.edit.PageSelection; import com.ironsoftware.ironpdf.image.ToImageOptions; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.nio.file.Paths; import java.util.List; public class main < public static void main(String[] args) throws IOException < PdfDocument instance = PdfDocument.fromFile(Paths.get("business plan.pdf")); ListextractedImages = instance.toBufferedImages(); ToImageOptions rasterOptions = new ToImageOptions(); rasterOptions.setImageMaxHeight(800); rasterOptions.setImageMaxWidth(500); List sizedExtractedImages = instance.toBufferedImages(rasterOptions, PageSelection.allPages()); int pageIndex = 1; for (BufferedImage extractedImage : sizedExtractedImages) < String fileName = "assets/images/" + pageIndex++ + ".png"; ImageIO.write(extractedImage, "PNG", new File(fileName)); >> >
import com.ironsoftware.ironpdf.PdfDocument; import com.ironsoftware.ironpdf.edit.PageSelection; import com.ironsoftware.ironpdf.image.ToImageOptions; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.nio.file.Paths; import java.util.List; public class main < public static void main(String[] args) throws IOException < PdfDocument instance = PdfDocument.fromFile(Paths.get("business plan.pdf")); ListextractedImages = instance.toBufferedImages(); ToImageOptions rasterOptions = new ToImageOptions(); rasterOptions.setImageMaxHeight(800); rasterOptions.setImageMaxWidth(500); List sizedExtractedImages = instance.toBufferedImages(rasterOptions, PageSelection.allPages()); int pageIndex = 1; for (BufferedImage extractedImage : sizedExtractedImages) < String fileName = "assets/images/" + pageIndex++ + ".png"; ImageIO.write(extractedImage, "PNG", new File(fileName)); >> >

The output of the following example will be saved in the assets folder of your project (create this folder before running the program) with numbering starting from 1 and will be incremented for all the PDF pages.

Java PDF to Image - Figure 5: New Project

5.2. Converting URL to PDF and PDF to Images

Using IronPDF for Java, you can convert HTML to PDF directly, and then convert each page of the generated PDF to images.

For the next example, we will use the Amazon website. The program below will render a page on Amazon.com into PDF and then output each page of the PDF as images stored in an assets folder.

import com.ironsoftware.ironpdf.PdfDocument; import com.ironsoftware.ironpdf.edit.PageSelection; import com.ironsoftware.ironpdf.image.ToImageOptions; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.nio.file.Paths; import java.util.List; public class main < public static void main(String[] args) throws IOException < PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://www.amazon.com/?tag=hp2-brobookmark-us-20"); ListextractedImages = pdf.toBufferedImages(); ToImageOptions rasterOptions = new ToImageOptions(); rasterOptions.setImageMaxHeight(800); rasterOptions.setImageMaxWidth(500); List sizedExtractedImages = pdf.toBufferedImages(rasterOptions, PageSelection.allPages()); int i = 1; for (BufferedImage extractedImage : sizedExtractedImages) < String fileName = "assets/images/" + i++ + ".png"; ImageIO.write(extractedImage, "PNG", new File(fileName)); >> >
import com.ironsoftware.ironpdf.PdfDocument; import com.ironsoftware.ironpdf.edit.PageSelection; import com.ironsoftware.ironpdf.image.ToImageOptions; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.nio.file.Paths; import java.util.List; public class main < public static void main(String[] args) throws IOException < PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://www.amazon.com/?tag=hp2-brobookmark-us-20"); ListextractedImages = pdf.toBufferedImages(); ToImageOptions rasterOptions = new ToImageOptions(); rasterOptions.setImageMaxHeight(800); rasterOptions.setImageMaxWidth(500); List sizedExtractedImages = pdf.toBufferedImages(rasterOptions, PageSelection.allPages()); int i = 1; for (BufferedImage extractedImage : sizedExtractedImages) < String fileName = "assets/images/" + i++ + ".png"; ImageIO.write(extractedImage, "PNG", new File(fileName)); >> >

Java PDF to Image - Figure 6: PDF to Images Output

Adjust the resolution of the images created from the example above by replacing the calls to the ToImageOptions instance with the ones below:

rasterOptions.setImageMaxHeight(800); rasterOptions.setImageMaxWidth(500);
rasterOptions.setImageMaxHeight(800); rasterOptions.setImageMaxWidth(500);

The two lines of code above adjusts the width and height that each of the generated images will have when toBufferedImage is invoked.

6. Conclusion

This How-To Guide shows how to convert PDFs into images using IronPDF for Java. The PDF page images produced from IronPDF contain both the page number and the name of the document, as shown in the previous examples. IronPDF can generate images in different formats: JPEG, JPG, TIFF and many more.

IronPDF also gives full control over the output image resolution to its users. To know more about IronPDF for Java and to access additional How-To Guides on how to manipulate PDF using Java, please refer to the following link. For more information about how to convert a PDF to images, go to this link.

IronPDF for Java is free for development purposes. but requires a license for commercial use. Get more additional information about the license here.

You can download the software product from this link.

On This Page

Источник

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

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(); > > 

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(); > > 

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(); > > 

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"); > > 

Источник

Оцените статью