将每个TIFF帧转换为JPEG格式,并使用Java将其转换为PDF格式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将每个TIFF帧转换为JPEG格式,并使用Java将其转换为PDF格式相关的知识,希望对你有一定的参考价值。

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.
  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 = readers.next();
  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(ir.read(i), "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");
  16.  
  17.  
  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();
  24.  
  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();
  29.  
  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
  43. pdf1.save("d:/pdftest/JPEG_image_toPDF.pdf");
  44. // close InputStream holding TIFF image
  45. iis.close();

以上是关于将每个TIFF帧转换为JPEG格式,并使用Java将其转换为PDF格式的主要内容,如果未能解决你的问题,请参考以下文章

delphi:怎么把JPEG、BMP,wmf等Timage支持的格式转化为TIFF?

在python中将tiff转换为jpeg

在node.js和GraphicsMagick中将tiff转换为jpeg

GDAL:Python GDAL.Translate可以将TIFF文件转换为JPEG或PNG数组 - 而不是写入文件吗?

以编程方式为 Silverlight 显示选择图像转换格式为 JPEG 或 PNG

在 .NET 2.0 中将位图转换为一个多页 TIFF 图像