Post: [Java Tutorial # 3] Convert TIFF Frames to JPEG and then transform it to PDF Format
01-27-2014, 06:07 AM #1
(adsbygoogle = window.adsbygoogle || []).push({}); In my last tutorial, I shared the You must login or register to view this content.. In this Tutorial I am going to share code for converting each TIFF frame to JPEG and then you can transform/save that JPEG file as PDF. I am using Aspose.PDF for Java library for this task you can use the library you want or you can also try this code by using Aspose Trial version. The reason for sharing this code is to give you an idea about how to perform the above task.


    
// read the source TIFF image
ImageInputStream iis = ImageIO.createImageInputStream(new FileInputStream("d:/pdftest/BW_MultiPage_TIFF.tif"));
Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
// create ImageReader object to parse the Image
ImageReader ir = readers.next();
// specify the input source
ir.setInput(iis);
// get the count of frames in TIFF image
int frameCount = ir.getNumImages(true);
// iterate through each TIFF frame
for (int i = 0; i < frameCount; ++i)
// save the individual TIFF frame as separate image with JPEG compression
ImageIO.write(ir.read(i), "jpg", new File("d:/pdftest/TIFF_JPEG_Coversion/frame" + i + ".jpg"));
// show the total count of tiff frames (optional)
// System.out.println("Tiff contains: "+frameCount+" frames");


// specify the directory containing JPEG images
String image_Directory = "d:/pdftest/TIFF_JPEG_Coversion/";
// read the files from Image directory
File f = new File(image_Directory);
// get the list of files in directory
File[] fa = f.listFiles();

//Instantiate a Pdf object by calling its empty constructor
Pdf pdf1 = new Pdf();
//Create a section in the Pdf object
Section sec1 = pdf1.getSections().add();

for (int i = 0; i < fa.length; i++)
{
// by default each image will be added as new paragraph on new page
//Create an image object in the section
aspose.pdf.Image img1 = new aspose.pdf.Image(sec1);
//Add image object into the Paragraphs collection of the section
sec1.getParagraphs().add(img1);
//Set the path of image file
img1.getImageInfo().setFile(fa[i].getPath());
//Set the image file Type
img1.getImageInfo().setImageFileType(ImageFileType.Jpeg);
}
//Save the Pdf
pdf1.save("d:/pdftest/JPEG_image_toPDF.pdf");
// close InputStream holding TIFF image
iis.close();

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo