import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import org.apache.commons.io.IOUtils;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.RandomAccessFileOrArray;
import com.itextpdf.text.pdf.codec.TiffImage;
public class TiffToPdfConverter
{
public static void main(String args[])
{
TiffToPdfConverter converter=new TiffToPdfConverter();
String[] files=new String[]{"D:\\TestTiffToPDF\\AZ-MC-DYI-2002.1034.tif","D:\\TestTiffToPDF\\testcolor-packbits.tif","D:\\TestTiffToPDF\\03.tif"};
try
{
byte[] bytes1=converter.printImagesToPDF(files);
OutputStream out1=new FileOutputStream("D:\\TestTiffToPDF\\test-fax2.pdf");
out1.write(bytes1);
FileInputStream inputStream=new FileInputStream(new File("D:\\TestTiffToPDF\\AZ-MC-DYI-2002.1034.tif"));
FileInputStream inputStream1=new FileInputStream(new File("D:\\TestTiffToPDF\\03.tif"));
FileInputStream inputStream2=new FileInputStream(new File("D:\\TestTiffToPDF\\testcolor-packbits.tif"));
byte[] bytes=IOUtils.toByteArray(inputStream);
byte[] bytes2=IOUtils.toByteArray(inputStream1);
byte[] bytesInputStream2=IOUtils.toByteArray(inputStream2);
ArrayList fileBytes=new ArrayList();
fileBytes.add(bytes);
fileBytes.add(bytes2);
fileBytes.add(bytesInputStream2);
byte[] bytes3=converter.printImagesToPDF(fileBytes);
OutputStream out2=new FileOutputStream("D:\\TestTiffToPDF\\test-fax3.pdf");
out2.write(bytes3);
} catch (Exception e)
{
e.printStackTrace();
}
}
private byte[] printImagesToPDF(String[] files){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
Document TifftoPDF = new Document(PageSize.A4, 0, 0, 0, 0);
PdfWriter.getInstance(TifftoPDF,baos );
TifftoPDF.open();
for(int i=0;i
Image tempImage=TiffImage.getTiffImage(getInputFileInBytes(files[i]), 1);
tempImage.scaleToFit(595f, 842f);
TifftoPDF.add(tempImage);
}
TifftoPDF.close();
} catch (DocumentException e) {
e.printStackTrace();
}
return baos.toByteArray();
}
private byte[] printImagesToPDF(ArrayList fileBytes) throws IOException{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
RandomAccessFileOrArray myTiffFile=null;
try {
Document TifftoPDF = new Document(PageSize.A4, 0, 0, 0, 0);
PdfWriter.getInstance(TifftoPDF,baos );
TifftoPDF.open();
for(int i=0;i
ByteArrayInputStream byteInputStream=new ByteArrayInputStream((byte[])fileBytes.get(i));
myTiffFile=new RandomAccessFileOrArray(byteInputStream);
Image tempImage=TiffImage.getTiffImage(myTiffFile, 1);
tempImage.scaleToFit(595f, 842f);
TifftoPDF.add(tempImage);
}
TifftoPDF.close();
} catch (DocumentException e) {
e.printStackTrace();
}
return baos.toByteArray();
}
private RandomAccessFileOrArray getInputFileInBytes(String fileName){
RandomAccessFileOrArray myTiffFile=null;
try{
//Read the Tiff File
FileInputStream inputStream=new FileInputStream(new File(fileName));
byte[] bytes=IOUtils.toByteArray(inputStream);
ByteArrayInputStream byteInputStream=new ByteArrayInputStream(bytes);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
myTiffFile=new RandomAccessFileOrArray(byteInputStream);
}
catch (Exception i1){
i1.printStackTrace();
}
return myTiffFile;
}
}