Convert Image To PDF File Using Java

Introduction

This tutorial will show you how to convert image to pdf file using Java programming language. For this I am using here itext API. The example Java image to pdf file will show you step by step procedures for image to pdf conversion. I will build the application using both gradle and maven tools.

In this example I will align images in the center of the generated pdf page. If the image width goes outside of pdf page, then I will make the image fit within the size of the output pdf document page.

Prerequisites

Java 1.8+, Maven 3.6.3 – 3.8.2, Gradle 6.4.1, itextpdf 5.5.13.1 – 5.5.13.1

image to pdf

Project Setup

You can create gradle or maven based project in Eclipse. The name of the project is java-image-to-pdf.

If you are creating gradle based project then you can use below build.gradle script.

plugins {
    id 'java-library'
}

repositories {
    jcenter()
}

dependencies {
    implementation 'com.itextpdf:itextpdf:5.5.13.1'
}

If you are creating maven based project then you can use below pom.xml file.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.roytuts</groupId>
	<artifactId>java-image-to-pdf</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>8</maven.compiler.source>
		<maven.compiler.target>8</maven.compiler.target>
	</properties>

	<dependencies>
		<dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>itextpdf</artifactId>
			<version>5.5.13.2</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
			</plugin>
		</plugins>
	</build>
</project>

Related Posts:

Convert Image to Pdf using Java

The following Java class will take image as input and produce pdf file as an output.

Here I am adding two images into the list and the output pdf file will have two pages with images. The second image goes out of the pdf page width so we have put conditional statement to fit the image into the pdf page.

public class ImageToPdfConverter {

	public static void main(String arg[]) throws Exception {
		convertImageToPdf("C:/files/input", "C:/files/output", "output.pdf");
	}

	public static void convertImageToPdf(final String fileInputPath, final String fileOuputPath,
			final String outputFileName) throws DocumentException, IOException {
		Document document = new Document();
		PdfWriter.getInstance(document, new FileOutputStream(new File(fileOuputPath, outputFileName)));
		document.open();

		List<File> files = Files.list(Paths.get(fileInputPath)).map(Path::toFile).collect(Collectors.toList());

		files.forEach(f -> {
			document.newPage();

			Image image = null;
			try {
				image = Image.getInstance(new File(fileInputPath, f.getName()).getAbsolutePath());
			} catch (Exception e) {
				e.printStackTrace();
			}

			image.setAlignment(Element.ALIGN_CENTER);

			float imageWidth = image.getWidth();

			if (imageWidth > document.getPageSize().getWidth()) {
				int indentation = 0;
				float scaler = ((document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin()
						- indentation) / image.getWidth()) * 100;
				image.scalePercent(scaler);
			}

			try {
				document.add(image);
			} catch (Exception e) {
				e.printStackTrace();
			}
		});

		document.close();
	}

}

Here I am adding two images into the list and the output pdf file will have two pages with images. The second image goes out of the pdf page width, so I have put conditional statement to fit the image into the pdf page.

In the above source code, I iterate through all files from a given directory using Java’s new file IO API, and I have also defined the output file’s destination directory where I want to generate the pdf file.

Then I store all the image files which need to be added to the same pdf file.

Testing the Image to PDF Conversion

Input Images

java image to pdf
java image to pdf

Output Pdf

Output pdf file can be found in the source code.

Source Code

Download

1 thought on “Convert Image To PDF File Using Java

Leave a Reply

Your email address will not be published.