PDFOne (for Java)
Create, edit, view, print & enhance PDF documents and forms in Java SE/EE
Compatibility
J2SE J2EE Windows Linux Mac (OS X)

Convert Multi-Page TIFF To PDF And Vice Versa In Java

Learn to render individual pages of multipage TIFF image on PDF pages.
By V. Subhash

In an earlier PDFOne (for Java™) article, you learned how to rasterize a PDF document by first exporting all PDF page content as images and then rendering those images on the pages of a new PDF document.

A somewhat similar requirement is to convert a multi-page TIFF to a PDF document. In this case, you need to extract individual pages of the multi-page TIFF image and then render them on the pages of a new PDF document. In this article, you will see how to do this with Gnostice PDFOne. Here is the multi-page TIFF image used for this article.

Convert From Multi-Page TIFF To PDF

In the following code example, first extract individual pages of a TIFF image as a java.awt.image.RenderedImage instance and then create a com.gnostice.pdfone.PdfImage instance. After that, rendering those PdfImage instances on the PDF document is easy.

import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;

import com.gnostice.pdfone.PdfDocument;
import com.gnostice.pdfone.PdfException;
import com.gnostice.pdfone.PdfImage;
import com.gnostice.pdfone.PdfMeasurement;
import com.gnostice.pdfone.PdfPage;
import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.SeekableStream;
import com.sun.media.jai.codec.TIFFDecodeParam;

public class TIFF_To_PDF {

  public static void main(String[] args) throws IOException, PdfException {
    PdfDocument doc = new PdfDocument();
    doc.setMeasurementUnit(PdfMeasurement.MU_PIXELS);

    // Gain seekable access to the multi-page TIFF image
    SeekableStream seekableStream1 =
      new FileSeekableStream(new File("sample_image.tiff"));

    // Create an TIFF image decoder for the image
    ImageDecoder imageDecoder1 =
        ImageCodec.createImageDecoder(
            "tiff",
            seekableStream1,
            (TIFFDecodeParam) null);

    // Count number of pages in the image
    int n = imageDecoder1.getNumPages();

    // Parse individual pages in the TIFF image
    for (int i = 0; i < n; i++) {

        // Access current image
        RenderedImage renderedImage = imageDecoder1.decodeAsRenderedImage(i);

        // Create a new PDF page with same dimensions as current image
        PdfPage page = new PdfPage(
                            renderedImage.getWidth(),
                            renderedImage.getHeight(),
                            PdfMeasurement.MU_PIXELS);
        // Draw image on the page
        page.drawImage(PdfImage.create(renderedImage), 0, 0);

        // Add the page to the document
        doc.add(page);
    }

    // Save document to file
    doc.save("tiff_to_pdf_demo.pdf");
  }
}

And, finally, here is the output PDF document.

Convert From PDF To Multi-Page TIFF

Gnostice Team member Abhishek Abuja reports that a lot of people request for the reverse form of this conversion - from PDF to TIFF. We refrained from writing a separate article, as the job can be done with a single method call - PdfDocument.saveDocAsTiffImage(). He provides the following code snippet for those looking for a simple example.

import java.io.IOException;

import com.gnostice.pdfone.PdfDocument;
import com.gnostice.pdfone.PdfException;

public class SaveDocAsTIFFDemo {
  public static void main(String[] args) throws IOException, PdfException {
    try {
      // Create a new PdfDocument instance
      PdfDocument d = new PdfDocument();

      // Load a PDF document
      d.load(args[0]);

      // Export pages to a multi page tiff image
      d.saveDocAsTiffImage(args[1]);

      // Save the changes to file
      d.close();

    } catch (ArrayIndexOutOfBoundsException e) {
      System.out.println("java DocAsTiffImageTest  ");
    }
  }
}

Did you add JAI and JAI Image IO libraries?

Gnostice team member Santhanam says that several people have tried the above code without adding the Java Advanced Imaging and Java Advanced Imaging Image IO libraries. Without importing the JARs, the code will not work.

You can download the JARs from:

---o0O0o---

Our .NET Developer Tools
Gnostice Document Studio .NET

Multi-format document-processing component suite for .NET developers.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
Gnostice Document Studio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
Gnostice Document Studio Java

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2024 Gnostice Information Technologies Private Limited. All rights reserved.