Combining multiple images into a single PDF document is an efficient method for those who want to store or distribute their images in a more organized way. Converting these images into a single PDF file not only saves storage space but also ensures that all images are kept together in one place, making it easier and more convenient to share or transfer them. In this article, you will learn how to merge several images into a single PDF document in Java using Spire.PDF for Java.
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 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.8.1</version> </dependency> </dependencies>
Convert Multiple Images into a Single PDF Document in Java
In order to convert all the images in a folder to a PDF, we iterate through each image, add a new page to the PDF with the same size as the image, and then draw the image onto the new page. The following are the detailed steps.
- Create a PdfDocument object.
- Set the page margins to zero using PdfDocument.getPageSettings().setMargins() method.
- Get the folder where the images are stored.
- Iterate through each image file in the folder, and get the width and height of a specific image.
- Add a new page that has the same width and height as the image to the PDF document using PdfDocument.getPages().add() method.
- Draw the image on the page using PdfPageBase.getCanvas().drawImage() method.
- Save the document using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.PdfDocument; import com.spire.pdf.PdfPageBase; import com.spire.pdf.graphics.PdfImage; import java.awt.*; import java.io.File; public class ConvertMultipleImagesIntoPdf { public static void main(String[] args) { //Create a PdfDocument object PdfDocument doc = new PdfDocument(); //Set the page margins to 0 doc.getPageSettings().setMargins(0); //Get the folder where the images are stored File folder = new File("C:/Users/Administrator/Desktop/Images"); //Iterate through the files in the folder for (File file : folder.listFiles()) { //Load a particular image PdfImage pdfImage = PdfImage.fromFile(file.getPath()); //Get the image width and height int width = pdfImage.getWidth(); int height = pdfImage.getHeight(); //Add a page that has the same size as the image PdfPageBase page = doc.getPages().add(new Dimension(width, height)); //Draw image at (0, 0) of the page page.getCanvas().drawImage(pdfImage, 0, 0, width, height); } //Save to file doc.saveToFile("CombineImagesToPdf.pdf"); doc.dispose(); } }
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.