sc.emea.tools.image.Tiff2PDF.java Source code

Java tutorial

Introduction

Here is the source code for sc.emea.tools.image.Tiff2PDF.java

Source

/*
 * Copyright 2014 Peter METZ, Steelcase Werndl AG, Germany
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package sc.emea.tools.image;

import java.io.File;
import java.io.FileInputStream;
//We need the library below to write the final 
//PDF file which has our image converted to PDF
import java.io.FileOutputStream;

//Document object to add logical image files to PDF
import com.itextpdf.text.Document;
//The image class to extract separate images from Tiff image
import com.itextpdf.text.Image;
import com.itextpdf.text.io.RandomAccessSource;
import com.itextpdf.text.io.RandomAccessSourceFactory;
//PdfWriter object to write the PDF document
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.RandomAccessFileOrArray;
//Read Tiff File, Get number of Pages
import com.itextpdf.text.pdf.codec.TiffImage;

import sc.emea.tools.image.FileList;
import sc.emea.tools.image.FileList.ComparableAttribute;
import sc.emea.tools.image.FileList.SortingDirection;

public class Tiff2PDF {
    public static void main(String args[]) {

        int page_counter = 0;

        try {

            // Read the Tiff File
            FileList myFileList = new FileList("C:\\DEV\\Eclipse-Workspace\\Luna-J\\Tiff2PdfConverter\\TIFF\\");
            myFileList.setFilterPreMiddleSuffix("", "", ".tiff");
            myFileList.sort(ComparableAttribute.dateLastModified, SortingDirection.asc);

            File[] myTiffFileFiles = myFileList.getFilesArray();

            if (myTiffFileFiles.length < 1)
                return;
            else
                for (File file : myTiffFileFiles)
                    System.out.println(file.getName());

            Document myDocument = new Document();
            myDocument.setMargins(0, 0, 0, 0);
            PdfWriter.getInstance(myDocument,
                    new FileOutputStream("C:\\DEV\\Eclipse-Workspace\\Luna-J\\Tiff2PdfConverter\\PDF\\test.pdf"));
            myDocument.open();

            RandomAccessSourceFactory rasFactory = new RandomAccessSourceFactory();

            for (File file : myTiffFileFiles) {

                FileInputStream _fis = new FileInputStream(file.getAbsolutePath());

                RandomAccessSource _ra_source = rasFactory.createSource(_fis);
                RandomAccessFileOrArray _ra_file = new RandomAccessFileOrArray(_ra_source);

                int numberOfPages = TiffImage.getNumberOfPages(_ra_file);
                for (int i = 1; i <= numberOfPages; i++) {
                    Image _image = TiffImage.getTiffImage(_ra_file, i);
                    _image.scaleAbsolute(myDocument.getPageSize());
                    myDocument.add(_image);
                    page_counter++;
                }

            }
            myDocument.close();
            System.out.println("Tiff to PDF conversion completed for " + page_counter);

        } catch (Exception i1) {
            i1.printStackTrace();
        }
    }
}