JAI funky tiff dpi makes pdf conversion scrunched to half page

D

dwilson

Hello Everybody,

I am trying to convert various tiff images into pdf, and have
encountered a problem when the tiff image has a funky dpi, i.e.
x-dpi=203 & y-dpi=96. The image is scrunched to a half page towards the
bottom of the pdf. The temp tiff created is also scrunched to about
half. I was looking for a way to compensate for this, so that it takes
up the whole page. A link to this image
http://www.cranberymedia.net/danw/1388242_1.FOB3.tif and here is my
code...

import java.awt.image.RenderedImage;
import java.io.*;
import java.util.*;

import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import com.sun.media.jai.codec.*;
import com.sun.media.jai.codecimpl.*;

public class test {

public static void main(String[] args) {
go();
}

private static void go() {
try {
File imageFile = new File("c:\\work\\1388242_1.FOB3.tif");
SeekableStream ss = new FileSeekableStream(imageFile);
ImageDecoder decoder = ImageCodec.createImageDecoder("tiff", ss,
null);
int count = decoder.getNumPages();
InputStream images[] = new InputStream[count];
for (int j = 0; j < decoder.getNumPages(); j++) {
TIFFEncodeParam param = new TIFFEncodeParam();
param.setCompression(TIFFEncodeParam.COMPRESSION_GROUP3_1D);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
RenderedImage rImage = decoder.decodeAsRenderedImage(j);

TIFFImageEncoder encoder = (TIFFImageEncoder) TIFFCodec
.createImageEncoder("tiff", baos, param);
encoder.encode(rImage);
baos.toByteArray();
InputStream inputStream = new ByteArrayInputStream(baos
.toByteArray());
images[j] = inputStream;
}

FileOutputStream fout = new FileOutputStream("c:\\work\\bpdf.pdf");

java.util.List pages = new ArrayList();
Rectangle pagesize = PageSize.LETTER;

for (int i = 0; i < images.length; i++) {
InputStream tempInputStream = images;
File tempIs = File.createTempFile("pdfTempIn"
+ (new Date()).getTime() + "" + i, null);
tempIs.deleteOnExit();
FileOutputStream outputStream = new FileOutputStream(tempIs);
int b = tempInputStream.read();
while (b != -1) {
outputStream.write(b);
b = tempInputStream.read();
}
outputStream.flush();
outputStream.close();
tempInputStream.close();
tempInputStream = new FileInputStream(tempIs);
RenderedOp image1 = JAI.create("stream", SeekableStream
.wrapInputStream(tempInputStream, true));
Image img = Image.getInstance(image1.getAsBufferedImage(),
null, true);
img.scaleToFit(pagesize.width(), pagesize.height());
img.setAbsolutePosition(0, 0);
pages.add(img);
tempInputStream.close();
}
int leftMargin = 0;
int rightMargin = 0;
int topMargin = 0;
int bottomMargin = 0;
Document document = new Document(pagesize, leftMargin, rightMargin,
topMargin, bottomMargin);
PdfWriter writer = PdfWriter.getInstance(document, fout);
writer.open();
document.open();
PdfContentByte cb = writer.getDirectContent();
Iterator pi = pages.iterator();
while (pi.hasNext()) {
Image img = (Image) pi.next();
cb.addImage(img);
document.newPage();
}
document.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("done");
System.exit(0);
}
}

Any insight on this issue would be greatly appreciated.

dwilson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top