Schreiben von Dokumenten als TIFF

Mit der jadice document platform und dem Zusatzmodul TIFFConverter ist es möglich jedes geladene Jadice-Dokument (Instanzen, welche das Interface com.levigo.jadice.document.Document implementieren) in ein TIFF Dokument zu schreiben.

Das folgende Code-Fragment demonstriert die Verwendung des TIFFConverter. Es wurde als Task Implementiert um über einen com.levigo.util.concurrent.tasks.TaskService ausgeführt zu werden.

ConvertDocumentTask.java
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> {

  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;
  }

}