DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Convert Files to Thumbnail Images in Java
  • Understanding Floating-Point Precision Issues in Java
  • How To Convert Common Documents to PNG Image Arrays in Java
  • How To Convert ODF Files to PDF in Java

Trending

  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • Beyond Manual Mapping: AI-Orchestrated Governance for Hybrid Cloud
  • Build a Voice Assistant With Gemini Live API
  • Spring Security JWT Validation Best Practices

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. Convert TIFF Images to PDF/JPEG in Java

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.

By 
Shrisowdhaman Selvaraj
·
Dec. 03, 15 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
36.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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);
        writer.open();

        document.open();

        String fileName = tiff;
        Iterator readers = javax.imageio.ImageIO
                .getImageReadersBySuffix("tiff");
        if (readers.hasNext()) {
            File fi = new File(fileName);
            ImageInputStream iis = javax.imageio.ImageIO
                    .createImageInputStream(fi);
            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(
                            bufferedImage.getWidth(),
                            bufferedImage.getHeight(),
                            BufferedImage.TYPE_INT_RGB);

                    // 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);
                    baos.flush();
                    // Convert byteArrayoutputSteam to ByteArray
                    byte[] imageInByte = baos.toByteArray();
                    document.add(Image.getInstance(imageInByte));
                    baos.close();

                }
                document.close();
            }
        }
    }

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

Convert (command) Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • How to Convert Files to Thumbnail Images in Java
  • Understanding Floating-Point Precision Issues in Java
  • How To Convert Common Documents to PNG Image Arrays in Java
  • How To Convert ODF Files to PDF in Java

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook