Umwandlung von PDF, um mehrseitige tiff (Group 4)

Ich bin versucht zu konvertieren von PDF-Dateien, dargestellt durch die org.apache.pdfbox.pdmodel.PDDocument Klasse und die icafe-Bibliothek (https://github.com/dragon66/icafe/) zu einer multipage-tiff mit group 4 Kompression und 300 dpi. Der Beispielcode funktioniert bei mir für 288 dpi, aber seltsamerweise NICHT für 300 dpi, die exportierten tiff-bleibt einfach weiß. Hat jemand eine Idee, was das Problem hier ist?

Den Beispiel-pdf, welches ich in dem Beispiel befindet sich hier: http://www.bergophil.ch/a.pdf

import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;

import cafe.image.ImageColorType;
import cafe.image.ImageParam;
import cafe.image.options.TIFFOptions;
import cafe.image.tiff.TIFFTweaker;
import cafe.image.tiff.TiffFieldEnum.Compression;
import cafe.io.FileCacheRandomAccessOutputStream;
import cafe.io.RandomAccessOutputStream;

public class Pdf2TiffConverter {
    public static void main(String[] args) {
        String pdf = "a.pdf";
        PDDocument pddoc = null;
        try {
            pddoc = PDDocument.load(pdf);
        } catch (IOException e) {
        }

        try {
            savePdfAsTiff(pddoc);
        } catch (IOException e) {
        }
    }

    private static void savePdfAsTiff(PDDocument pdf) throws IOException {
        BufferedImage[] images = new BufferedImage[pdf.getNumberOfPages()];
        for (int i = 0; i < images.length; i++) {
            PDPage page = (PDPage) pdf.getDocumentCatalog().getAllPages()
                    .get(i);
            BufferedImage image;
            try {
//             image = page.convertToImage(BufferedImage.TYPE_INT_RGB, 288); //works
                image = page.convertToImage(BufferedImage.TYPE_INT_RGB, 300); //does not work
                images[i] = image;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        FileOutputStream fos = new FileOutputStream("a.tiff");
        RandomAccessOutputStream rout = new FileCacheRandomAccessOutputStream(
                fos);
        ImageParam.ImageParamBuilder builder = ImageParam.getBuilder();
        ImageParam[] param = new ImageParam[1];
        TIFFOptions tiffOptions = new TIFFOptions();
        tiffOptions.setTiffCompression(Compression.CCITTFAX4);
        builder.imageOptions(tiffOptions);
        builder.colorType(ImageColorType.BILEVEL);
        param[0] = builder.build();
        TIFFTweaker.writeMultipageTIFF(rout, param, images);
        rout.close();
        fos.close();
    }
}

Oder gibt es eine andere Bibliothek zu schreiben, die multi-page-TIFFs?

EDIT:

Dank dragon66 der Fehler in icafe ist nun behoben. In der Zwischenzeit habe ich experimentierte mit anderen Bibliotheken und auch mit der Anrufung ghostscript. Wie ich denke, dass ghostscript ist sehr zuverlässig, wie die id ist ein weit verbreitetes Werkzeug, auf der anderen Seite habe ich angewiesen, dass der Benutzer meines Codes hat eine ghostscript-installation so etwas wie dieses:

   /**
 * Converts a given pdf as specified by its path to an tiff using group 4 compression
 *
 * @param pdfFilePath The absolute path of the pdf
 * @param tiffFilePath The absolute path of the tiff to be created
 * @param dpi The resolution of the tiff
 * @throws MyException If the conversion fails
 */
private static void convertPdfToTiffGhostscript(String pdfFilePath, String tiffFilePath, int dpi) throws MyException {
    //location of gswin64c.exe
    String ghostscriptLoc = context.getGhostscriptLoc();

    //enclose src and dest. with quotes to avoid problems if the paths contain whitespaces
    pdfFilePath = "\"" + pdfFilePath + "\"";
    tiffFilePath = "\"" + tiffFilePath + "\"";

    logger.debug("invoking ghostscript to convert {} to {}", pdfFilePath, tiffFilePath);
    String cmd = ghostscriptLoc + " -dQUIET -dBATCH -o " + tiffFilePath + " -r" + dpi + " -sDEVICE=tiffg4 " + pdfFilePath;
    logger.debug("The following command will be invoked: {}", cmd);

    int exitVal = 0;
    try {
        exitVal = Runtime.getRuntime().exec(cmd).waitFor();
    } catch (Exception e) {
        logger.error("error while converting to tiff using ghostscript", e);
        throw new MyException(ErrorMessages.GHOSTSTSCRIPT_ERROR, e);
    }
    if (exitVal != 0) {
        logger.error("error while converting to tiff using ghostscript, exitval is {}", exitVal);
        throw new MyException(ErrorMessages.GHOSTSTSCRIPT_ERROR);
    }
}

Fand ich, dass die produzierte tif aus ghostscript unterscheidet sich stark in der Qualität von der tiff produziert von icafe (Gruppe 4 tiff aus ghostscript sieht Graustufen-like)

Ich würde empfehlen, auch zu öffnen ein Problem ist hier: github.com/dragon66/icafe/issues/new
und wo finde ich die aktuellsten icafe.jar? Oder muss ich es bauen mich?
vielen Dank, es funktioniert jetzt. Ich experimentierte auch mit ghostscript (mit -sDEVICE=tiffg4) und ich Frage mich, warum das Ergebnis so anders ist (vor allem für Bilder, für text-der Unterschied ist nicht so Auffällig). Ghostscript erzeugt TIFF-Dateien, die sehen eher aus wie greylevel (obwohl es ist bilevel), während icafe produziert eher "hässlichen" schwarz und weiß Bilder
Hinweis: ICAFE jetzt können Sie einstellen, dpi, für vertikale und horizontale Richtungen
Danke Icafe ist toll, auf diese ...

InformationsquelleAutor Raphael Roth | 2015-08-12

Schreibe einen Kommentar