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.
Why Convert TIFF to PDF?
Before diving into code, let’s look at why you might need to convert TIFF files to PDF:
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.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.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.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
Post a Comment