Convert Each TIFF Frame to JPEG & Transform them to PDF Format Using Java

/ Published in: Java
Save to your folder(s)

This technical tip shows how each TIFF frame can be converted to JPEG and then these images to PDF file using Aspose.Pdf for Java. 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.

Copy this code and paste it in your HTML
  1. // read the source TIFF image
  2. ImageInputStreamiis = ImageIO.createImageInputStream(new FileInputStream("d:/pdftest/BW_MultiPage_TIFF.tif"));
  3. Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
  4. // create ImageReader object to parse the Image
  5. ImageReaderir =;
  6. // specify the input source
  7. ir.setInput(iis);
  8. // get the count of frames in TIFF image
  9. intframeCount = ir.getNumImages(true);
  10. // iterate through each TIFF frame
  11. for (int i = 0; i <frameCount; ++i)
  12. // save the individual TIFF frame as separate image with JPEG compression
  13. ImageIO.write(, "jpg", new File("d:/pdftest/TIFF_JPEG_Coversion/frame" + i + ".jpg"));
  14. // show the total count of tiff frames (optional)
  15. // System.out.println("Tiff contains: "+frameCount+" frames");
  18. // specify the directory containing JPEG images
  19. String image_Directory = "d:/pdftest/TIFF_JPEG_Coversion/";
  20. // read the files from Image directory
  21. File f = new File(image_Directory);
  22. // get the list of files in directory
  23. File[] fa = f.listFiles();
  25. //Instantiate a Pdf object by calling its empty constructor
  26. Pdf pdf1 = new Pdf();
  27. //Create a section in the Pdf object
  28. Section sec1 = pdf1.getSections().add();
  30. for (int i = 0; i <fa.length; i++)
  31. {
  32. // by default each image will be added as new paragraph on new page
  33. //Create an image object in the section
  34. aspose.pdf.Image img1 = new aspose.pdf.Image(sec1);
  35. //Add image object into the Paragraphs collection of the section
  36. sec1.getParagraphs().add(img1);
  37. //Set the path of image file
  38. img1.getImageInfo().setFile(fa[i].getPath());
  39. //Set the image file Type
  40. img1.getImageInfo().setImageFileType(ImageFileType.Jpeg);
  41. }
  42. //Save the Pdf
  44. // close InputStream holding TIFF image
  45. iis.close();


Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.