Good morning,
i’m tring to convert some multi page tiff to a multi page pdf, but when I call the method save on the image frame object it never answers.
Here a class with a main that I used to reproduce the case:
package ...;
import ....UnsupportedTiffColorSpaceException;
import com.aspose.imaging.Image;
import com.aspose.imaging.ImageOptionsBase;
import com.aspose.imaging.PixelFormat;
import com.aspose.imaging.fileformats.tiff.TiffFrame;
import com.aspose.imaging.fileformats.tiff.TiffImage;
import com.aspose.imaging.fileformats.tiff.enums.TiffExpectedFormat;
import com.aspose.pdf.Page;
import com.aspose.pdf.PageSize;
import com.aspose.pdf.Rectangle;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import java.io.*;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class TestAspose {
public static void main(String[] args) {
try {
// DirectoryStream<Path> pathList = Files.newDirectoryStream(Paths.get("path/to/file/to/convert"));
DirectoryStream<Path> pathList = Files.newDirectoryStream(Paths.get("D:\\temp\\converter"));
for (Path path : pathList) {
File file = path.toFile();
if (file.isDirectory()) {
continue;
}
ByteArrayInputStream docPath = null;
try {
docPath = new ByteArrayInputStream(FileUtils.readFileToByteArray(file));
} catch (IOException e) {
System.out.println("error reading file: " + file.getAbsolutePath());
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (Image image = Image.load(docPath)) {
if (image.getFileFormat() != com.aspose.imaging.FileFormat.Tiff &&
image.getFileFormat() != com.aspose.imaging.FileFormat.BigTiff) {
return;
}
Image[] images = null;
boolean hasAlpha = false;
if (image instanceof TiffImage tiffImage) {
images = tiffImage.getPages();
hasAlpha = tiffImage.hasAlpha();
} else {
images = new Image[]{image};
}
try (com.aspose.pdf.Document document = new com.aspose.pdf.Document()) {
for (Image imageTmp : images) {
InputStream stream = null;
ByteArrayOutputStream workingStream = null;
try (Page page = document.getPages().add()) {
float scale = PageSize.getA4().getHeight() /
Integer.max(imageTmp.getWidth(), imageTmp.getHeight());
float scaledWidth = imageTmp.getWidth() * scale;
float scaledHeight = imageTmp.getHeight() * scale;
page.setPageSize(scaledWidth, scaledHeight);
Rectangle rectangle = new Rectangle(0, 0, scaledWidth, scaledHeight);
page.setMediaBox(rectangle);
workingStream = new ByteArrayOutputStream();
int compressionType;
if (imageTmp instanceof TiffFrame frame) {
int pixelFormat = frame.getRawDataSettings().getPixelDataFormat().getPixelFormat();
if (pixelFormat == PixelFormat.Cmyk) {
compressionType = hasAlpha || frame.hasAlpha() ? TiffExpectedFormat.TiffNoCompressionCmyk : TiffExpectedFormat.TiffNoCompressionCmyka;
} else if (pixelFormat == PixelFormat.Rgb) {
compressionType = hasAlpha || frame.hasAlpha() ? TiffExpectedFormat.TiffNoCompressionRgb : TiffExpectedFormat.TiffNoCompressionRgba;
} else if (pixelFormat == PixelFormat.Grayscale) {
compressionType = TiffExpectedFormat.TiffNoCompressionBw;
} else {
throw new UnsupportedTiffColorSpaceException("file name");
}
} else {
throw new UnsupportedTiffColorSpaceException("file name");
}
try (ImageOptionsBase options = new com.aspose.imaging.imageoptions.TiffOptions(compressionType)) {
imageTmp.save(workingStream, options);
}
stream = new ByteArrayInputStream(workingStream.toByteArray());
page.addImage(stream, rectangle);
} finally {
IOUtils.closeQuietly(workingStream);
IOUtils.closeQuietly(stream);
IOUtils.closeQuietly(imageTmp);
}
}
com.aspose.pdf.optimization.OptimizationOptions optimizationOptions =
new com.aspose.pdf.optimization.OptimizationOptions();
optimizationOptions.setRemoveUnusedObjects(true);
optimizationOptions.setRemoveUnusedStreams(true);
optimizationOptions.setUnembedFonts(true);
document.optimizeResources(optimizationOptions);
document.optimize();
document.save(outputStream);
//to do you can save the output to a file...
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.exit(0);
}
}
P.S.: I also tried to use Aspose’s online image editor product here, but still stuck also there.
I’m using aspose total 24.1, I will provide some examples shortly, thanks in advacne to everyone for support.