Hello,
i'm using this
program to convert a tiff file to a pdf on the fly, but it doesn't work, can you
help please :
protected
ByteArrayOutputStream tiff2Pdf(
final HttpServletRequest req, File file,
final ServletContext ctx)
throws DocumentException{
ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
// creation of the document with a certain size and certain margins
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
//Document.compress = false;
try {
// creation of the different writers
PdfWriter writer = PdfWriter.getInstance(document, baosPDF);
SeekableStream s = new FileSeekableStream(file);
TIFFDecodeParam param = null;
ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param);
System.out.println("Number of images in this TIFF: " + dec.getNumPages());
// Which of the multiple images in the TIFF file do we want to load
// 0 refers to the first, 1 to the second and so on.
int total = dec.getNumPages();
document.open();
PdfContentByte cb = writer.getDirectContent();
for (int k = 0; k < total; ++k) {
RenderedImage ri = dec.decodeAsRenderedImage(k);
Raster ra = ri.getData();
BufferedImage bi = new BufferedImage(ri.getColorModel(),
Raster.createWritableRaster(ri.getSampleModel(),
ra.getDataBuffer(), null), false, new Hashtable());
final HttpServletRequest req, File file,
final ServletContext ctx)
throws DocumentException{
ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
// creation of the document with a certain size and certain margins
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
//Document.compress = false;
try {
// creation of the different writers
PdfWriter writer = PdfWriter.getInstance(document, baosPDF);
SeekableStream s = new FileSeekableStream(file);
TIFFDecodeParam param = null;
ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param);
System.out.println("Number of images in this TIFF: " + dec.getNumPages());
// Which of the multiple images in the TIFF file do we want to load
// 0 refers to the first, 1 to the second and so on.
int total = dec.getNumPages();
document.open();
PdfContentByte cb = writer.getDirectContent();
for (int k = 0; k < total; ++k) {
RenderedImage ri = dec.decodeAsRenderedImage(k);
Raster ra = ri.getData();
BufferedImage bi = new BufferedImage(ri.getColorModel(),
Raster.createWritableRaster(ri.getSampleModel(),
ra.getDataBuffer(), null), false, new Hashtable());
Image img =
Image.getInstance(bi, null,
true);
img.scalePercent(72f / 200f * 100);
img.setAbsolutePosition(0, 0);
System.out.println("Image: " + k);
cb.addImage(img);
document.newPage();
}
document.close();
}
catch (Exception de) {
de.printStackTrace();
//System.err.println(de.getMessage());
}
return baosPDF;
}
img.scalePercent(72f / 200f * 100);
img.setAbsolutePosition(0, 0);
System.out.println("Image: " + k);
cb.addImage(img);
document.newPage();
}
document.close();
}
catch (Exception de) {
de.printStackTrace();
//System.err.println(de.getMessage());
}
return baosPDF;
}
