I followed this example of iText 7 to convert a multi-page Tiff into a multi-page PDF, but when I open the PDF it's corrupted. Adobe Reader displays an error and Chrome shows this:
(Every page looks like that, but they aren't identical).
This is the code I used:
File newPdfFile = new File("<path...>/converted_file.pdf");
URL tiffUrl = UrlUtil.toURL("<path...>/original_file.tif");
IRandomAccessSource ras = new RandomAccessSourceFactory().createSource(tiffUrl);
RandomAccessFileOrArray rafoa = new RandomAccessFileOrArray(ras);
int numberOfPages = TiffImageData.getNumberOfPages(rafoa);
PdfDocument pdf = new PdfDocument(new PdfWriter(new FileOutputStream(newPdfFile)));
Document document = new Document(pdf);
for(int i = 1; i <= numberOfPages; ++i) {
Image image = new Image(ImageDataFactory.createTiff(tiffUrl, true, i, true));
document.add(image);
}
document.close();
pdf.close();
And this is the code I used with iText 5.5.11, which works but uses a deprecated constructor of RandomAccessFileOrArray
:
File newPdfFile = new File("<path...>/converted_file.pdf");
RandomAccessFileOrArray rafoa = new RandomAccessFileOrArray("<path...>/original_file.tif");
int numberOfPages = TiffImage.getNumberOfPages(rafoa);
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(newPdfFile));
document.open();
for (int i = 1; i <= numberOfPages; ++i) {
Image image = TiffImage.getTiffImage(rafoa, i);
Rectangle pageSize = new Rectangle(image.getWidth(), image.getHeight());
document.setPageSize(pageSize);
document.newPage();
document.add(image);
}
document.close();
Unfortunately I can't provide sample files because they are confidential/classified...
What could be the issue?
P.S.: I tried with the same tiff used in the example code I followed and it works. What's wrong with my tiffs? In the file properties, other than the dimensions and resolution there's:
Ok, thanks to Michaël Demey's suggestions I managed to get the proper pdf using iText 7.
Here's the maven imports:
<dependency>
<groupId>com.sun.media</groupId>
<artifactId>jai_imageio</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>layout</artifactId>
<version>7.0.3</version>
</dependency>
And here's the code:
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
[...]
File newPdfFile = new File("<path...>/converted_file.pdf");
ImageReader reader = ImageIO.getImageReadersByFormatName("TIFF").next();
reader.setInput(ImageIO.createImageInputStream(new File("<path...>/original_file.tif")));
int numberOfPages = reader.getNumImages(true);
PdfDocument pdf = new PdfDocument(new PdfWriter(new FileOutputStream(newPdfFile)));
Document document = new Document(pdf);
for(int i = 0; i < numberOfPages; ++i) {// in javax.imageio.ImageReader they start from 0!
java.awt.Image img = reader.read(i);
Image tempImage = new Image(ImageDataFactory.create(img, null));
pdf.addNewPage(new PageSize(tempImage.getImageWidth(), tempImage.getImageHeight()));
tempImage.setFixedPosition(i + 1, 0, 0);
document.add(tempImage);
}
document.close();
pdf.close();