Skip to main content

How to Convert TIFF Images to PDF Using Java

In daily work, we often need to handle files in different formats. Among them, TIFF (Tagged Image File Format) is a high-quality image format widely used for scanned images, faxes, medical images, and other scenarios where image quality is crucial. However, while TIFF offers excellent image fidelity, PDF is often preferred for file sharing, printing, and archiving due to its better compatibility and convenience.

In this article, we’ll walk you through how to convert TIFF images to PDF using Java, including examples for both single-file and batch conversions.

convert tiff to pdf in java

Why Convert TIFF to PDF?

Before diving into code, let’s look at why you might need to convert TIFF files to PDF:

  1. Better Compatibility
    TIFF files offer high image quality, but not all platforms can view them easily. PDF, on the other hand, is a universal standard that works across almost all operating systems and devices without relying on specific image viewers. Converting TIFF to PDF ensures your files can be viewed smoothly everywhere.

  2. Simplified Sharing and Archiving
    PDF files are usually smaller than TIFF images, saving storage space. They also support metadata, password protection, and digital signatures, making them more secure and manageable for sharing and archiving.

  3. Improved Printing Support
    PDFs maintain their layout and formatting when printed, regardless of printer drivers or image viewers. TIFF files, however, may face scaling or compatibility issues during printing. This is why many organizations prefer converting TIFFs to PDFs for consistent printing results.

  4. Efficient Batch Processing
    For users handling large volumes of images, batch converting TIFF files to PDF can save significant time. PDFs allow automated generation and customizable page layouts for each image, streamlining workflows.

Environment Setup

To convert TIFF to PDF in Java, we’ll use the Spire.PDF for Java library. Before coding, make sure you’ve set up the library. You can download the JAR files from the official site or use Maven to add dependencies.

Maven Dependencies:

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>12.2.1</version>
    </dependency>
</dependencies>

Converting a Single TIFF to PDF in Java

The process is straightforward: load the TIFF image and draw it onto a PDF page. Here’s the step-by-step guide:

Step 1: Create a PdfDocument Object

The PdfDocument class is the core class in Spire.PDF for creating and managing PDF files.

PdfDocument pdf = new PdfDocument();

This object will be used to create and manipulate the PDF document.

Step 2: Add a PDF Page

Add a new blank page to the PDF document:

PdfPageBase page = pdf.getPages().add();

Step 3: Load the TIFF Image

Use PdfImage.fromFile() to load the TIFF image:

PdfImage tiff = PdfImage.fromFile("sample.tiff");

This creates a PdfImage object representing the TIFF image.

Step 4: Draw the Image on the PDF Page

Use the page’s canvas to draw the image:

page.getCanvas().drawImage(tiff, 0, 0, 580, 800);

Here, (0,0) sets the top-left position, and 580x800 specifies the image size.

Step 5: Save the PDF

Finally, save the PDF to disk:

pdf.saveToFile("TiffToPdf.pdf", FileFormat.PDF);

You can also save the PDF to a stream if needed.

Complete Example Code for Single TIFF to PDF Conversion

import com.spire.pdf.*;
import com.spire.pdf.graphics.PdfImage;

public class TiffToPDF {
    public static void main(String[] args) {
        // Create PdfDocument object
        PdfDocument pdf = new PdfDocument();

        // Add a new page
        PdfPageBase page = pdf.getPages().add();

        // Load TIFF image as PdfImage
        PdfImage tiff = PdfImage.fromFile("sample.tiff");

        // Draw image on PDF page
        page.getCanvas().drawImage(tiff, 0, 0, 580, 800);

        // Save PDF file
        pdf.saveToFile("TiffToPdf.pdf", FileFormat.PDF);
    }
}

Batch Converting TIFF Images to PDF in Java

In real-world scenarios, you often need to convert multiple TIFF files into a single PDF document. Here’s how to do it:

Step 1: Get All TIFF Files from a Folder

Use the File class to access all TIFF files in a folder.

Step 2: Add Each TIFF as a New Page

For each image, create a new page in the PDF and draw the image.

Batch TIFF to PDF Conversion Example

import com.spire.pdf.*;
import com.spire.pdf.graphics.PdfImage;
import java.io.File;

public class BatchTiffToPDF {
    public static void main(String[] args) {
        // Create PdfDocument object
        PdfDocument pdf = new PdfDocument();

        // Load all TIFF files from a folder
        File folder = new File("tiff_images");
        File[] files = folder.listFiles((dir, name) -> name.endsWith(".tiff"));

        // Loop through each TIFF file
        for (File file : files) {
            // Add a new page
            PdfPageBase page = pdf.getPages().add();

            // Load TIFF image
            PdfImage tiff = PdfImage.fromFile(file.getAbsolutePath());

            // Draw image on PDF page
            page.getCanvas().drawImage(tiff, 0, 0, 580, 800);
        }

        // Save PDF
        pdf.saveToFile("BatchTiffToPdf.pdf", FileFormat.PDF);
    }
}

In this example, each TIFF file from the tiff_images folder is converted into a separate page in a single PDF file called BatchTiffToPdf.pdf.

Conclusion

Converting TIFF images to PDF in Java is simple and efficient. Whether you need to convert a single image or batch process multiple files, the method outlined here allows for easy customization, such as adjusting image size or handling different image types. By following this approach, you can streamline your workflows, improve file compatibility, and ensure your images are ready for sharing, archiving, or printing.

Comments

Popular posts from this blog

3 Ways to Generate Word Documents from Templates in Java

A template is a document with pre-applied formatting like styles, tabs, line spacing and so on. You can quickly generate a batch of documents with the same structure based on the template. In this article, I am going to show you the different ways to generate Word documents from templates programmatically in Java using Free Spire.Doc for Java library. Prerequisite First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that. If you use maven, you need to add the following code to your project’s pom.xml file. <repositories>               <repository>                   <id>com.e-iceblue</id>                   <name>e-iceblue</name>...

Insert and Extract OLE objects in Word in Java

You can use OLE (Object Linking and Embedding) to include content from other programs, such as another Word document, an Excel or PowerPoint document to an existing Word document. This article demonstrates how to insert and extract embedded OLE objects in a Word document in Java by using Free Spire.Doc for Java API.   Add dependencies First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that. If you use maven, you need to add the following code to your project’s pom.xml file.     <repositories>               <repository>                   <id>com.e-iceblue</id>                   <name>e-iceblue</name>    ...

Simple Java Code to Convert Excel to PDF in Java

This article demonstrates a simple solution to convert an Excel file to PDF in Java by using free Excel API – Free Spire.XLS for Java . The following examples illustrate two possibilities to convert Excel to PDF:      Convert the whole Excel file to PDF     Convert a particular Excel Worksheet to PDF Before start with coding, you need to Download Free Spire.XLS for Java package , unzip it and import Spire.Xls.jar file from the lib folder in your project as a denpendency. 1. Convert the whole Excel file to PDF Spire.XLS for Java provides saveToFile method in Workbook class that enables us to easily save a whole Excel file to PDF. import com.spire.xls.FileFormat; import com.spire.xls.Workbook; public class ExcelToPDF {     public static void main(String[] args){         //Create a Workbook         Workbook workbook = new Workbook();   ...