1
votes

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:

Screenshot

(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:

  • Bit depth: 1
  • Compression: CCITT T.4
  • Resolution unit: 2
2
The issue is in the tiff file. But if you can't provide it, then we can't help you. My suggestions are to try and preprocess the tiff by loading it into JAI (Java Advanced Imaging) and then passing the Java image to iText.Michaël Demey
How do I pass it to iText, exactly? All the constructors and methods I'm using require either a URL, a RandomAccessFileOrArray or a byte[]. What do I need to do with JAI so the byte[] it gives me is different than the original? Or is there another way to pass the image to iText?nonzaprej
JAI outputs a BufferedImage, right? Output the BufferedImage to a byte[] and you're set.Michaël Demey
I thought so but I wasn't sure... ok now I gotta get Maven to find that damned jai_imageio library... Thanks.nonzaprej

2 Answers

1
votes

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();
0
votes

I had also faced the same issue .I was able to convert file which is of "TIFF" extension and with that of "TIF" extension i was not able to get it converted properly .Then i tried something that was able to do it.Try changing as below and check from

Image image = new Image(ImageDataFactory.createTiff(tiffUrl, true, i, true));

to

Image image = new Image(ImageDataFactory.createTiff(tiffUrl, true, i, false));

This made my conversion work.Hope it works for you as well