Bethan Palmer Bethan is a Java developer and a Java Champion. She has spoken at conferences including JavaOne/Code One, DevFest and NetBeans days. She has a degree in English Literature.

How to convert an image to a PDF in Java

2 min read

In a previous post I looked at why you might want to convert a PDF file to an image (you can use JPedal to do this). This time I will look at doing the opposite, and explain why and how to convert an image to a PDF file.

Why convert image to PDF?

Some images can be displayed in the browser, and do not require PDF display tools to be viewed. Converting images to PDF files allows the display of Tiff and other non-support image types and makes them easier to handle. PDF files can be altered and extra content can be added to them. Converting an image to a PDF will also reduce the size of the file.

The PDF file format is very complex and we generally recommend using existing tools rather than trying to build your own from scratch (if you really want to try this we have a series of articles on how to do this). We recommend you use a Java library. There are lots of Open Source and Commercial Java libraries and we have 2 examples below.

1. iText (Open source library)

One way to convert an image to a PDF in Java is to use iText. iText is a PDF generation and manipulation tool for Java. It allows you to create a new PDF document and then add an existing image to that document.

You can find example code for adding an image to a PDF document using iText here.

2. JDeli (Commercial library)

Another way to convert an image to a PDF in Java is to use JDeli, our Java image library. JDeli can be used to read and write a large number of image file formats in Java. It can also be used to write an image to a PDF file.

How to convert an image file to a PDF file in JDeli

Step 1 Add JDeli to your class or module path. (link to the trial jar).
Step 2 Create a File (or InputStream) object for the Image input file.

File imageFile = new File("C:\\path\\to\\pdf\\exampleImage.tif");

Step 3 Create a PdfEncoderOptions object for control over PDF file output.

PdfEncoderOptions options = new PdfEncoderOptions();
JDeli.write(bufferedImage, outputOptions, file);

Step 4 Create a File (or OutputStream) object

File pdfFile = new File("C:\\path\\to\\pdf\\examplePDF.pdf");

Step 5 Pass input File object, Encoder Options and output File object into write method

//create new PDF or append if PDF already exists
JDeli.write(imageFile, outputOptions, pdfFile);

If the image is a multi-page TIFF then JDeli will generate a multi-page PDF file, with as many images as the TIFF contains.

How to convert a Java BufferedImage to a PDF file in JDeli

Step 1 Add JDeli to your class or module path. (link to the trial jar).
Step 2 Create a File (or OutputStream) object

File pdfFile = new File("C:\\path\\to\\pdf\\examplePDF.pdf");

Step 3 Pass image and File object into write method

//create new PDF or append if PDF already exists
JDeli.write(bufferedImage, file);

If the image is a multi-page TIFF then JDeli will generate a multi-page PDF file, with as many images as the TIFF contains.

Why use JDeli to write Image to PDF?

JDeli offers a range of advantages over ImageIO and alternatives, including:

  • prevents heap related JVM crashes
  • support for multiple image file formats including Heic
  • reduce output file size
  • improve write performance
  • supports threading

JDeli can also read, write and process a large range of image file formats. Learn more about JDeli, or download to try it yourself.

If you found this guide useful, you may be interested in our series of articles on handling common image file formats in Java.

Did you know...

IDRsolutions offers a whole range of online file converters to convert PDF and Microsoft Excel, Word and Office Documents to HTML5, SVG or image formats?

It is free to use for single file conversions and also includes Developer links if you want to use our commercial software for bulk conversions. Find out more on this page

Bethan Palmer Bethan is a Java developer and a Java Champion. She has spoken at conferences including JavaOne/Code One, DevFest and NetBeans days. She has a degree in English Literature.

How to read HEIC image files in Java with…

In this article, I will explain how to read HEIC files into Java as a BufferedImage. ImageIO does not read HEIC file types so...
Mark Stephens
1 min read

How to convert WMF files to SVG in java…

This article will show you how to convert WMF files into SVG files using our JDeli Java Image library. What is WMF? WMF is...
Amy Pearson
1 min read

How to write WebP images in Java

In this article, I will walk you through how to write out images as WebP images in Java. ImageIO does not support WebP images...
Mark Stephens
1 min read

Leave a Reply

Your email address will not be published. Required fields are marked *

IDRsolutions Ltd 2020. All rights reserved.