if (getValue("destfile") == null) throw new InstantiationException("You need to choose a destination file");
File pdf_file = (File)getValue("destfile");
RandomAccessFileOrArray ra = new RandomAccessFileOrArray(tiff_file.getAbsolutePath());
int comps = TiffImage.getNumberOfPages(ra);
boolean adjustSize = false;
LwgDocument document = new LwgDocument(LwgPageSize.A4);
float width = LwgPageSize.A4.getWidth() - 40;
float height = LwgPageSize.A4.getHeight() - 120;
if ("ORIGINAL".equals(getValue("pagesize"))) {
LwgImage img = TiffImage.getTiffImage(ra, 1);
if (img.getDpiX() > 0 && img.getDpiY() > 0) {
img.scalePercent(7200f / img.getDpiX(), 7200f / img.getDpiY());
}
document.setPageSize(new LwgRectangle(img.getScaledWidth(), img.getScaledHeight()));
adjustSize = true;
}
else if ("LETTER".equals(getValue("pagesize"))) {
document.setPageSize(LwgPageSize.LETTER);
width = LwgPageSize.LETTER.getWidth() - 40;
height = LwgPageSize.LETTER.getHeight() - 120;
}
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdf_file));
document.open();
PdfContentByte cb = writer.getDirectContent();
for (int c = 0; c < comps; ++c) {
LwgImage 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 LwgRectangle(img.getScaledWidth(),
img.getScaledHeight()));
document.newPage();
img.setAbsolutePosition(0, 0);
}
else {
if (img.getScaledWidth() > width || img.getScaledHeight() > height) {
if (img.getDpiX() > 0 && img.getDpiY() > 0) {
float adjx = width / img.getScaledWidth();
float adjy = height / img.getScaledHeight();
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));
}
}
ra.close();
document.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(internalFrame,
e.getMessage(),
e.getClass().getName(),
JOptionPane.ERROR_MESSAGE);