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
- Add JDeli to your class or module path. (link to the trial jar).
- Create a File (or InputStream) object for the Image input file.
- Create a PdfEncoderOptions object for control over PDF file output.
- Create a File (or OutputStream) object
- 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…
File imageFile = new File("C:\\path\\to\\pdf\\exampleImage.tif");
PdfEncoderOptions options = new PdfEncoderOptions();
File pdfFile = new File("C:\\path\\to\\pdf\\examplePDF.pdf");
//create new PDF or append if PDF already exists
JDeli.convert(imageFile, options, pdfFile);
How to convert a Java BufferedImage to a PDF file
- Add JDeli to your class or module path. (link to the trial jar).
- Create a File (or OutputStream) object
- Create a PdfEncoderOptions object for control over PDF file output
- Pass image and File object into write method
and the Java code to convert a Java BufferedImage to PDF…
File pdfFile = new File("C:\\path\\to\\pdf\\examplePDF.pdf");
PdfEncoderOptions options = new PdfEncoderOptions();
//create new PDF or append if PDF already exists
JDeli.write(bufferedImage, options, file);
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?
// Read an image
BufferedImage bufferedImage = JDeli.read(dicomImageFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(heicImageFile);
// Write an image
JDeli.write(bufferedImage, "heic", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(jpegImageFile);
// Write an image
JDeli.write(bufferedImage, "jpeg", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(jpeg2000ImageFile);
// Write an image
JDeli.write(bufferedImage, "jpx", outputStreamOrFile);
// Write an image
JDeli.write(bufferedImage, "pdf", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(pngImageFile);
// Write an image
JDeli.write(bufferedImage, "png", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(tiffImageFile);
// Write an image
JDeli.write(bufferedImage, "tiff", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(webpImageFile);
// Write an image
JDeli.write(bufferedImage, "webp", outputStreamOrFile);
What is JDeli?
JDeli is a commercial Java Image library that is used to read, write, convert, manipulate and process many different image formats.
Why use JDeli?
To handle many well known formats such as JPEG, PNG, TIFF as well as newer formats like AVIF, HEIC and JPEG XL in java with no calls to any external system or third party library.
What licenses are available?
We have 3 licenses available:
Server for on premises and cloud servers, Distribution for use in a named end user applications, and Custom for more demanding requirements.
How does JDeli compare?
We work hard to make sure JDeli performance is better than or similar to other java image libraries. Check out our benchmarks to see just how well JDeli performs.