将每个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.
// read the source TIFF image ImageInputStreamiis = ImageIO.createImageInputStream(new FileInputStream("d:/pdftest/BW_MultiPage_TIFF.tif")); Iterator<ImageReader> readers = ImageIO.getImageReaders(iis); // create ImageReader object to parse the Image ImageReaderir = readers.next(); // specify the input source ir.setInput(iis); // get the count of frames in TIFF image intframeCount = 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 // show the total count of tiff frames (optional) // System.out.println("Tiff contains: "+frameCount+" frames"); // specify the directory containing JPEG images // read the files from Image directory // get the list of files in directory //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 //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();
以上是关于将每个TIFF帧转换为JPEG格式,并使用Java将其转换为PDF格式的主要内容,如果未能解决你的问题,请参考以下文章
delphi:怎么把JPEG、BMP,wmf等Timage支持的格式转化为TIFF?
在node.js和GraphicsMagick中将tiff转换为jpeg
GDAL:Python GDAL.Translate可以将TIFF文件转换为JPEG或PNG数组 - 而不是写入文件吗?