import java.io.File;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.RandomAccessFileOrArray;
import
com.lowagie.text.pdf.codec.TiffImage;
public class Tiff2Pdf {
public static void main(String[] args) {
String tiff_file;
String pdf_file;
String path = "T:\\tii2pdf";
String newpath = "T:\\outputpdf";
File Dir = new File(path);
File[] files =
Dir.listFiles();
int count = 0;
//for (int i = 0; i < args.length; i++) {
for (int m = 0;m < files.length;m++){
tiff_file = files[m].getName();
//tiff_file = "C:\\TEST.TIF";
pdf_file = tiff_file.substring(0, tiff_file.lastIndexOf(".") + 1) + "pdf";
Document document = new Document();
try {
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream(newpath+"/"+pdf_file));
int pages = 0;
document.open();
PdfContentByte cb = writer.getDirectContent();
RandomAccessFileOrArray ra = null;
int comps = 0;
try {
ra = new RandomAccessFileOrArray(path+"/"+tiff_file);
comps = TiffImage.getNumberOfPages(ra);
}
catch (Throwable e) {
System.out.println("Exception in " + tiff_file + " " +
e.getMessage());
continue;
}
System.out.println("Processing: " + tiff_file + ":"+ ++count);
for (int c = 0; c < comps; ++c) {
try {
Image img = TiffImage.getTiffImage(ra, c + 1);
if (img != null) {
System.out.println("page " + (c + 1));
/* if (img.scaledWidth() > 500 || img.scaledHeight() > 700) {
img.scaleToFit(500, 700);
} */
if (img.scaledWidth() > 600 || img.scaledHeight() > 800) {
img.scaleToFit(600, 800);
}
img.setAbsolutePosition(20, 20);
//img.setAbsolutePosition(0, 0);
//document.add(new Paragraph(tiff_file + " - page " + (c + 1)));
cb.addImage(img);
document.newPage();
++pages;
}
}
catch (Throwable e) {
System.out.println("Exception " + tiff_file + " page " + (c + 1) + " " + e.getMessage());
}
}
ra.close();
document.close();
} catch (Throwable e) {
e.printStackTrace();
}
}
}
}
/***********/
I want the output.pdf to look exactly like input.tif, even fitting the page (no shrinking of image)
Dould you please guide me.
Thank you,
Aditya
