Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Convert TIFF Frames to Individual JPEG Images & Then to PDF Using Java

sheraz786sheraz786 Posts: 19Member

This technical tip shows how each TIFF frame can be converted to JPEG and then these images to PDF file using Aspose.Pdf. Aspose.Pdf for Java is very well capable of dealing with TIFF images and can easily transform them into PDF format. However there are cases when a TIFF image contains frames with different resolution or page orientation and product might not work well. So in order to resolve such issues, we can first convert TIFF frames into individual JPEG images and then save these images into PDF format using Aspose.Pdf for Java.

The following code snippet shows how each TIFF frame can be converted to JPEG and then these images to PDF file.

[Java Code Sample]

// read the source TIFF image ImageInputStream iis = ImageIO.createImageInputStream(new FileInputStream("d:/pdftest/BW_MultiPage_TIFF.tif")); Iterator 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();

Comments

Sign In or Register to comment.