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

1 min read

pdf logo

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 converting an image into a PDF document in Java.

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

  1. Add JDeli to your class or module path. (link to the trial jar).
  2. Create a File (or InputStream) object for the Image input file.
  3. Create a PdfEncoderOptions object for control over PDF file output.
  4. Create a File (or OutputStream) object
  5. Pass input File object, Encoder Options and output File object into write method

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.

and the Java code to convert image to PDF…

How to convert a Java BufferedImage to a PDF file

  1. Add JDeli to your class or module path. (link to the trial jar).
  2. Create a File (or OutputStream) object
  3. Create a PdfEncoderOptions object for control over PDF file output
  4. Pass image and File object into write method

and the Java code to convert a Java BufferedImage to PDF…

Why convert images 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 available to do this. For this tutorial, I will be using JDeli to do this. 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 into a PDF file.



Are you a Java Developer working with Image files?

Why do developers choose JDeli over free alternatives?

  1. Works with newer image formats such as AVIF, HEIC, JPEG XL, WEBP
  2. Better support than alternatives for JPEG, PNG, TIFF.
  3. Prevent JVM crashes caused by native code in other image libraries
  4. Better performance than other popular Java image libraries
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.