boolean adjustSize = false;
Document document = new Document(PageSize.A4);
float width = PageSize.A4.width() - 40;
float height = PageSize.A4.height() - 120;
if ("ORIGINAL".equals(getValue("pagesize"))) {
Image img = TiffImage.getTiffImage(ra, 1);
if (img.getDpiX() > 0 && img.getDpiY() > 0) {
img.scalePercent(7200f / img.getDpiX(), 7200f / img.getDpiY());
}
document.setPageSize(new Rectangle(img.scaledWidth(), img.scaledHeight()));
adjustSize = true;
}
else if ("LETTER".equals(getValue("pagesize"))) {
document.setPageSize(PageSize.LETTER);
width = PageSize.LETTER.width() - 40;
height = PageSize.LETTER.height() - 120;
}
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdf_file));
document.open();
PdfContentByte cb = writer.getDirectContent();
for (int c = 0; c < comps; ++c) {
Image img = TiffImage.getTiffImage(ra, c + 1);
if (img != null) {
if (img.getDpiX() > 0 && img.getDpiY() > 0) {
img.scalePercent(7200f / img.getDpiX(), 7200f / img.getDpiY());
}
if (adjustSize) {
document.setPageSize(new Rectangle(img.scaledWidth(),
img.scaledHeight()));
document.newPage();
img.setAbsolutePosition(0, 0);
}
else {
if (img.scaledWidth() > width || img.scaledHeight() > height) {
if (img.getDpiX() > 0 && img.getDpiY() > 0) {
float adjx = width / img.scaledWidth();
float adjy = height / img.scaledHeight();
float adj = Math.min(adjx, adjy);
img.scalePercent(7200f / img.getDpiX() * adj, 7200f / img.getDpiY() * adj);
}
else
img.scaleToFit(width, height);
}
img.setAbsolutePosition(20, 20);
document.newPage();
document.add(new Paragraph(tiff_file + " - page " + (c + 1)));
}
cb.addImage(img);
System.out.println("Finished page " + (c + 1));