How to Convert Between PDF and TIFF in Java
In this article, we highlight both the overlap between PDF and TIFF files in certain industries and suggest a few APIs to convert to and from each format.
Join the DZone community and get the full member experience.
Join For FreePDF and TIFF: Converting Between Document and Image in Java
We rarely encounter just one document format in enterprise applications. The longer a system has been in production, the more likely it is that file interoperability becomes a real concern. That’s especially true for file types that sit at the intersection of document and image processing, like PDF and TIFF.
TIFF and PDF are both widely used in healthcare, insurance, and legal services (among other industries) where a premium is placed upon long-term file fidelity and visual accuracy. While PDF has a much wider range of use-cases, TIFF holds ground in archival contexts and systems that prefer image-based representations over embedded formatting.
Introduction
In this article, we’ll look at what it means to convert between PDF and TIFF formats, in both directions. We’ll first break down how these formats store content, why that matters when we’re making a conversion between each format, and what kinds of considerations Java developers should keep in mind when switching between them.
Finally, we’ll explore some open-source and third-party APIs that Java developers can use to streamline these conversions in production code.
Understanding the (Significant) Structural Differences Between PDF and TIFF
PDF and TIFF are both designed for viewing static content, but they couldn’t be more different in terms of composition. TIFF is raster-based —meaning it stores image data pixel by pixel — while PDF is a container that can mix text, images, and vector graphics all in one file. A PDF can be one or more static, raster-based images, which is one way it overlaps with TIFF in terms of real-world functionality.
PDFs are designed to support layers, fonts, compression types, and even embedded scripts. TIFF files, on the other hand, lean heavily into visual fidelity and long-term preservation. Many TIFFs use CCITT or LZW compression and are commonly leaned upon for precision, such as single-bit black-and-white image scanning or medical imaging.
When we convert between TIFF and PDF, we’re not just changing file extensions — we’re changing how the file's internal contents are represented. That means we must choose how to properly convert PDFs into valid TIFF images, and how to encapsulate TIFF images into a valid PDF structure that still renders correctly across viewers.
Converting From PDF to TIFF: Rendering Pages as Image Data
When converting a PDF to TIFF, we’re effectively rendering each page of the PDF as an image, then encoding that image in lossless TIFF format. Under the hood, that involves selecting a rendering resolution (DPI), deciding whether to use grayscale, RGB, or black-and-white output, and finding a way to manage multipage TIFF creation if the original PDF has more than one page.
Probably the biggest consideration in this direction of our conversion is resolution. A higher DPI results in better visual fidelity, but also larger file sizes. For documents like scanned contracts or forms, a DPI of 200–300 is typically sufficient. For more detailed visual content, we might need to go higher.
Converting From TIFF to PDF: Wrapping Image Data in a Page Structure
When converting in the other direction — TIFF to PDF — the process flips: we’re taking big, detailed raster images and wrapping them in a valid PDF page structure. This means we’re making decisions about page sizes, margins, orientation, and lossy or lossless compression algorithms (e.g., using JPEG or Flate inside the PDF).
If the TIFF we’re converting from is a multipage image, we’ll need to create a separate PDF page for each frame of the TIFF. It's important to understand that we’re not embedding a TIFF inside a PDF — we’re re-encoding its contents into a different format entirely.
Open-Source Libraries for PDF-TIFF Conversion
For those looking to handle conversions to and from PDF and TIFF with open-source solutions, there are several established libraries that stand out.
PDF to TIFF With Apache PDFBox and JAI
Apache PDFBox is a robust and well-maintained library for working with PDF files in Java. Most importantly, it includes built-in support for rendering PDF pages to BufferedImage instances, which can then be encoded into TIFF format using the Java Advanced Imaging (JAI) API or standard ImageIO plugins. PDFBox gives us control over rendering DPI, color models, and page iteration, which makes it a solid choice for export workflows.
TIFF to PDF With iText or OpenPDF
To go from TIFF to PDF, we'll need a library that can build PDFs from image data. To that end, iText (and its fork, OpenPDF) offers a clean way to insert image content into new PDF documents, one image per page. With a multipage TIFF, we'd extract each frame as a separate image and append them sequentially to our resulting PDF. These libraries handle PDF layout and compression settings, which takes a lift off our hands. We can fine-tune the output as needed.
These tools don’t abstract away all the complexity, but they’re reliable, well-documented, and used in many production environments. They'll take us where we need to go.
Converting PDF to TIFF, TIFF to PDF With Web APIs
If we’re not looking to get intimately involved in open-source documentation, we can alternatively try a pair of free plug-and-play conversion APIs that integrate with our Java application using minimal code.
Code examples are provided below in this case. These aren’t open-source, which may be a no-go for some, but the idea here is to abstract and simplify all the cumbersome aspects of a high-fidelity conversion away from our environment entirely, including the overhead processing that would otherwise fall on our resources to support.
While not particularly relevant here, it's also worth noting that each of these APIs also supports conversions from a variety of other image formats (like WEBP, JPG, PNG, etc.).
PDF to TIFF
The first thing we'll do is install the SDK with Maven. We'll first add a reference to the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
And then we'll add a reference to the dependency in pom.xml:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
Following that, we'll add the import classes to the top of our file:
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ConvertApi;
Next, we'll initialize the API client, set an API key for authorization (we can get one of these for free), create a conversion API instance, and call the API with our PDF file input:
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
ConvertApi apiInstance = new ConvertApi();
File imageFile = new File("/path/to/inputfile"); // File | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
try {
byte[] result = apiInstance.convertToTiff(imageFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertApi#convertToTiff");
e.printStackTrace();
}
We'll get our TIFF image back as a byte array, which we can then write to a new TIFF file.
TIFF to PDF
We'll follow similar instructions here. We'll once again install the SDK with our pom.xml reference:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
And our pom.xml dependency:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
Then we'll add our imports:
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ConvertDocumentApi;
Finally, we'll initialize conversion iteration following the same overarching structure as the prior:
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
ConvertDocumentApi apiInstance = new ConvertDocumentApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
try {
byte[] result = apiInstance.convertDocumentAutodetectToPdf(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDocumentApi#convertDocumentAutodetectToPdf");
e.printStackTrace();
}
Similar to our other conversion, this will return a PDF byte array, which we can write to a PDF with the proper extension.
Conclusion
In this article, we explored the relationship between PDF and TIFF formats and how files can be programmatically converted between them. We suggested open-source and non-open-source API solutions to simplify the implementation of this conversion workflow in a Java environment.
Opinions expressed by DZone contributors are their own.
Comments