Search code examples
javapdfitexttiffitext7

iText7 to convert PDF to Tiff


I am able to convert PDF to TIFF using iText but it generates a TIFF file per page. I would like to generate one TIFF file with all the pages.

Here is my code:

final RenderingProperties properties = new RenderingProperties();
properties.setImageType(PdfRenderImageType.TIFF);
PdfToImageRenderer.renderPdf(new File(flatFileName), new File(destFileName)
    +"/my-custom-filename-%d", properties);

Solution

  • Multi-page TIFF output is not (yet) supported out of the box through a property setting. But it can be worked around quite easily, like this:

    BufferedImageReadyListener listener = new BufferedImageReadyListener() {
        TiffEncoder encoder = new TiffEncoder();
        @Override
        public void bufferedImageReady(BufferedImage bufferedImage, int pageNumber) {
            try {
                encoder.append(bufferedImage, destFileName);
            } catch (IOException e) {
                // handle exception
            }
        }
    };
    
    final RenderingProperties properties = new RenderingProperties();
    properties.setImageType(PdfRenderImageType.TIFF);
    PdfToImageRenderer.renderPdf(new File(flatFileName), properties, listener);