import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import com.levigo.jadice.document.Document;
import com.levigo.jadice.tiffconverter.TiffConvertConfiguration;
import com.levigo.jadice.tiffconverter.TiffConvertRenderSettings.Compression;
import com.levigo.jadice.tiffconverter.TiffConverter;
import com.levigo.util.concurrent.tasks.Task;
/**
* Convert a given {@link Document} to a TIFF file.
*/
public class ConvertDocumentTask implements Task<File> {
static {
// Due to a bug to the jadice document platform versions up to 5.1.0.3 (will be fixed with
// 5.1.0.4) this line has to be called to ensure proper initialization. This line must be
// removed after upgrading to a release later than 5.1.0.3
com.levigo.jadice.document.internal.codec.tiff.TagGroup.values();
}
private final Document document;
private final File targetFile;
public ConvertDocumentTask(Document document, File targetFile) {
super();
if (document == null)
throw new IllegalArgumentException("document must not be null");
this.document = document;
if (targetFile == null) {
try {
targetFile = File.createTempFile("jadice-documentplatform-tiffconverter-", ".tif");
} catch (IOException e) {
throw new RuntimeException("Failed to generate temporary file. Specify the output file explicitly.", e);
}
}
this.targetFile = targetFile;
}
@Override
public File call() throws Exception {
FileOutputStream out = new FileOutputStream(targetFile);
TiffConvertConfiguration convertConfiguration = new TiffConvertConfiguration();
// compression AUTO means that the tiff converter will use the most appropriate compression
// method. That is CCITT for B/W and JPEG for color.
convertConfiguration.setCompression(Compression.AUTO);
TiffConverter converter = new TiffConverter(convertConfiguration);
// the conversion must be surrounded by at least a read lock, to avoid that concurrent
// modifications cause the conversion to fail.
document.getPages().getReadWriteLock().readLock().lock();
try {
converter.convertToTiff(document, out, false);
} finally {
document.getPages().getReadWriteLock().readLock().unlock();
}
out.close();
return targetFile;
}
}
|