/*
* Copyright (C) 2011-2014 GeoForge Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.lowagie.geoforge.toolbox;
import com.lowagie.text.LwgDocument;
import com.lowagie.text.LwgImage;
import com.lowagie.text.LwgRectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfGState;
import com.lowagie.text.pdf.PdfPageLabels;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.toolbox.AbstractTool;
import com.lowagie.toolbox.arguments.AbstractArgument;
import java.io.File;
import java.io.FileOutputStream;
/**
*
* @author robert
*/
abstract public class GfrPdfPhotoAlbumFromAbs extends AbstractTool
{
final static private int _INT_DEFAULT_DPI_ = 72;
protected String _strPathAbsOut = null;
protected String _strMessageError = null;
public String getErrorMessage() { return this._strMessageError; }
// ---
protected GfrPdfPhotoAlbumFromAbs(String strPathAbsOut)
{
super();
this._strPathAbsOut = strPathAbsOut;
}
@Override
protected File getDestPathPDF() throws InstantiationException
{
return new File(this._strPathAbsOut);
}
@Override
protected void createFrame()
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void valueHasChanged(AbstractArgument arg)
{
throw new UnsupportedOperationException("Not supported yet.");
}
// ---
protected void _execute(File[] flesIn, String[] strsLabel)
{
try
{
LwgDocument document = new LwgDocument();
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream(this._strPathAbsOut));
writer.setViewerPreferences(PdfWriter.PageModeUseThumbs);
PdfPageLabels pageLabels = new PdfPageLabels();
for (int i=0; i<flesIn.length; i++)
{
System.out.println("Testing image: " + flesIn[i].getName());
LwgImage img = LwgImage.getInstance(flesIn[i].getAbsolutePath());
String caption = "";
int intDpiX = img.getDpiX();
if (intDpiX == 0)
intDpiX = GfrPdfPhotoAlbumFromAbs._INT_DEFAULT_DPI_;
int intDpiY = img.getDpiY();
if (intDpiY == 0)
intDpiY = GfrPdfPhotoAlbumFromAbs._INT_DEFAULT_DPI_;
float fltImgWidthPica = (GfrPdfPhotoAlbumFromAbs._INT_DEFAULT_DPI_ * img.getPlainWidth()) / intDpiX;
float fltImgHeightPica = (GfrPdfPhotoAlbumFromAbs._INT_DEFAULT_DPI_ * img.getPlainHeight()) / intDpiY;
img.scaleAbsolute(fltImgWidthPica, fltImgHeightPica);
document.setPageSize(new LwgRectangle(fltImgWidthPica, fltImgHeightPica));
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();
pageLabels.addPageLabel(writer.getPageNumber(),
PdfPageLabels.EMPTY, strsLabel[i]);
System.out.println("Added image: " + strsLabel[i]);
}
if (document.isOpen())
{
writer.setPageLabels(pageLabels);
document.close();
} // ??? else ???
}
catch(Exception exc)
{
exc.printStackTrace();
this._strMessageError = exc.getMessage();
}
}
}