throw new InstantiationException(
"You need to choose a destination file");
}
File pdf_file = (File) getValue("destfile");
LwgDocument document = new LwgDocument();
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream(pdf_file));
writer.setViewerPreferences(PdfWriter.PageModeUseThumbs);
PdfPageLabels pageLabels = new PdfPageLabels();
int dpiX, dpiY;
float imgWidthPica, imgHeightPica;
TreeSet<File> images = new TreeSet<File>();
File[] files = directory.listFiles();
if (files == null) {
throw new NullPointerException("listFiles() returns null");
}
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
images.add(files[i]);
}
}
String label;
for (File image: images) {
System.out.println("Testing image: " + image.getName());
try {
LwgImage img = LwgImage.getInstance(image.getAbsolutePath());
String caption = "";
dpiX = img.getDpiX();
if (dpiX == 0) {
dpiX = 72;
}
dpiY = img.getDpiY();
if (dpiY == 0) {
dpiY = 72;
}
imgWidthPica = (72 * img.getPlainWidth()) / dpiX;
imgHeightPica = (72 * img.getPlainHeight()) / dpiY;
img.scaleAbsolute(imgWidthPica, imgHeightPica);
document.setPageSize(new LwgRectangle(imgWidthPica,
imgHeightPica));
if (document.isOpen()) {
document.newPage();
} else {
document.open();
}
img.setAbsolutePosition(0, 0);
document.add(img);
BaseFont bf = BaseFont.createFont("Helvetica",
BaseFont.WINANSI,
false);
PdfGState gs1 = new PdfGState();
gs1.setBlendMode(PdfGState.BM_OVERLAY);
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
cb.setGState(gs1);
cb.beginText();
cb.setFontAndSize(bf, 40);
cb.setTextMatrix(50, 50);
cb.showText(caption);
cb.endText();
cb.restoreState();
label = image.getName();
if (label.lastIndexOf('.') > 0) {
label = label.substring(0, label.lastIndexOf('.'));
}
pageLabels.addPageLabel(writer.getPageNumber(),
PdfPageLabels.EMPTY, label);
System.out.println("Added image: " + image.getName());
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
if (document.isOpen()) {
writer.setPageLabels(pageLabels);
document.close();
} else {
System.err.println("No images were found in directory " +
directory.getAbsolutePath());
}