Over a million developers have joined DZone.

Convert TIFF Images to PDF/JPEG in Java

DZone's Guide to

Convert TIFF Images to PDF/JPEG in Java

This code snippet that uses the iText library can convert TIFF files into PDF or JPEG using Java.

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

To convert TIFF images to PDF/JPEG in Java, just use the iText PDF (version 5.1.3) library to read Tiff files and create a PDF from it.

This code will convert all TIFF files to PDF/JPG. The most common issue you will get is "Byte array is not a valid JPEG-file." & "jpeg 2000 Old format"

public static void TiffToJpg(String tiff, String output)
            throws IOException, DocumentException {

        // PDF conversation starts here
        Document document = new Document();

        FileOutputStream fos = new FileOutputStream(output);
        PdfWriter writer = PdfWriter.getInstance(document, fos);


        String fileName = tiff;
        Iterator readers = javax.imageio.ImageIO
        if (readers.hasNext()) {
            File fi = new File(fileName);
            ImageInputStream iis = javax.imageio.ImageIO
            TIFFDecodeParam param = null;
            ImageDecoder dec = ImageCodec.createImageDecoder("tiff", fi, param);
            int pageCount = dec.getNumPages();
            ImageReader _imageReader = (ImageReader) (readers.next());

            if (_imageReader != null) {
                _imageReader.setInput(iis, true);
                int count = 1;
                for (int i = 0; i < pageCount; i++) {
                    BufferedImage bufferedImage = _imageReader.read(i);

                    BufferedImage img2 = new BufferedImage(

                    // Set the RGB values for converted image (jpg)
                    for (int y = 0; y < bufferedImage.getHeight(); y++) {
                        for (int x = 0; x < bufferedImage.getWidth(); x++) {
                            img2.setRGB(x, y, bufferedImage.getRGB(x, y));

                    System.out.println("Page: " + (i + 1));
                    // Set the RGB values for converted image (jpg)

                   //To image
                      /*  String s = "C:/Users/TIFF/tiff"+i+".jpg";
                    ImageIO.write(img2, "jpg", new File(s));*/

                    //To pdf
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    ImageIO.write(img2, "jpg", baos);
                    // Convert byteArrayoutputSteam to ByteArray
                    byte[] imageInByte = baos.toByteArray();


Check the full source from https://github.com/shrisowdhaman/Tiff_to_PDF

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

java ,itext ,images

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}