javapdfboxtiff

Unable to add TIFF images into a PDF using PDFBox


I'm running into some issues in trying to create a PDF that consists of several TIF images using PDFBox 3.0.3. :( I tried several different approaches, but none have worked so far. I get essentially blank pages with a black line on each page. :(

Attempt 1

PDDocument document = new PDDocument();
for (int i=0;i<5;i++)
{
String sTIFFpath = sRootDir+i;
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
PDImageXObject image = PDImageXObject.createFromFile(sTIFFpath, document);
contentStream.drawImage(image, 0, 0);
contentStream.close();
}

The line that creates the image generates an exception. However, the image object is generated and retuned. Looking at the generated PDF I see essentially blank pages with a single blank line in them.

Here is the stack trace of the exception

2024-09-29 19:49:26,490 DEBUG PDImageXObject - Reading as TIFF failed, setting fileType to PNG
java.io.IOException: First image in tiff is not a single tile/strip
    at org.apache.pdfbox.pdmodel.graphics.image.CCITTFactory.extractFromTiff(CCITTFactory.java:439)
    at org.apache.pdfbox.pdmodel.graphics.image.CCITTFactory.createFromRandomAccessImpl(CCITTFactory.java:215)
    at org.apache.pdfbox.pdmodel.graphics.image.CCITTFactory.createFromFile(CCITTFactory.java:195)
    at org.apache.pdfbox.pdmodel.graphics.image.CCITTFactory.createFromFile(CCITTFactory.java:173)
    at org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject.createFromFileByExtension(PDImageXObject.java:236)
    at org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject.createFromFile(PDImageXObject.java:192)

To get around the exception, I replaced the line that generates the image with the following 2 lines:

BufferedImage biTiff = ImageIO.read(new File(sTIFFpath));
PDImageXObject image = CCITTFactory.createFromImage(document, biTiff);

That added quite a bit of time to my program execution but got rid of the exception. However the resulting PDF is the same. :(

After lots of searches I found yet another approach. Again I replaced the single line that creates the image with the following code multitude:

ImageInputStream is = ImageIO.createImageInputStream(new File(sTIFFpath));
Iterator<ImageReader> iterator = ImageIO.getImageReaders(is);
ImageReader reader = (ImageReader) iterator.next();
iterator = null;
reader.setInput(is);
int iImgCnt = reader.getNumImages(true);
for (int i=0; i < iImgCnt; i++)
{
BufferedImage biTiff = reader.read(i);
PDImageXObject img = CCITTFactory.createFromImage(document, biTiff);
//  PDImageXObject img = LosslessFactory.createFromImage(document, biTiff);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.drawImage(img, 0, 0);
contentStream.close();
page = new PDPage();
document.addPage(page);
}

No exceptions but the result is the same.


Solution

  • Change your Attempt 1 code:

    contentStream.drawImage(image, 0, 0);
    

    To:

    // Resize image to fit page
    contentStream.drawImage(image, 0, 0, page.getMediaBox().getWidth(), page.getMediaBox().getHeight());