< 戻る
印刷

PDFをGIFに変換

JPedalは、PDFファイルやPDFファイルのディレクトリをGIFに変換するための方法をいくつか提供しています。Javaの例では、ConvertPagesToImagesクラスとGifEncoderOptionsクラスを 使用します。

コマンドラインまたは他の言語からPDFをGIFに変換

java -jar jpedal.jar --convert "inputFileOrDir" "outputDir" gif

便利な静的メソッドでJavaでPDFをGIFに変換

ConvertPagesToImages.writeAllPagesAsImagesToDir("inputFileOrDir", "outputDir" , "gif", 1.33f);

画像出力を制御し、JavaでPDFをGIFに変換

ConvertPagesToImages convert = new ConvertPagesToImages("/path/to/file.pdf");
try {
    if (convert.openPDFFile()) {
        for(int page = 1; page <= convert.getPageCount(); page++) {
            final BufferedImage bi = convert.getPageAsImage(page);
            final File out = new File("/path/to/output/" + page + ".gif");
            // 出力を制御するセッター
            final GifEncoderOptions options = new GifEncoderOptions();
            JDeli.write(bi, options, out);
        }
    }
} catch (PdfException | IOException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}

convert.closePDFfile();

ページ範囲を制御してJavaでPDFをGIFに変換

ConvertPagesToImages convert = new ConvertPagesToImages("/path/to/file.pdf");
// setPageRangeは、範囲に'-'または':'を使用して希望のページを選択する機能を提供します
// また、次の範囲に移動する場合は','を使用します
convert.setPageRange(new PageRanges("1-5,8:10,15"));
// 上記の場合、1~5ページ、8~10ページ、15ページが表示されます
try {
    if (convert.openPDFFile()) {
        convert.getPageRange().forEachRemaining(page -> {
            try {
                final BufferedImage bi = convert.getPageAsImage(page);
                final File out = new File("/path/to/output/" + page + ".gif");
                JDeli.write(bi, options, out);
            } catch (Exception e) {
                e.printStackTrace();
            }
       });
    }
} catch (PdfException e) {
    e.printStackTrace();
}

convert.closePDFfile();

PDFから出力画像の寸法を調整したGIFサムネイルをJavaで変換

ConvertPagesToImages.
writeAllPagesAsImagesToDir("inputFileOrDir", "outputDir" , "gif", new int[]{width,height});

または

ConvertPagesToImages convert = new ConvertPagesToImages("/path/to/file.pdf");
//アスペクト比を維持したままフィット(幅は300、高さは400になります)
convert.setFitToSize(new int[]{300,400});
try {
    if (convert.openPDFFile()) {
        for (int page = 1; page <= convert.getPageCount(); page++) {
            final BufferedImage bi = convert.getPageAsImage(page);
            final File out = new File("/path/to/output/" + page + ".gif");
            JDeli.write(bi, OutputFormat.GIF, out);
        }
    }
} catch (PdfException | IOException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}

convert.closePDFfile();

PDFから拡大縮小を調整したGIFサムネイルをJavaで変換

ConvertPagesToImages convert = new ConvertPagesToImages("/path/to/file.pdf");
convert.setPageScaling(1.33f); //100%でAcrobatと同じサイズ
try {
    if (convert.openPDFFile()) {
        for (int page = 1; page <= convert.getPageCount(); page++) {
            final BufferedImage bi = convert.getPageAsImage(page);
            final File out = new File("/path/to/output/" + page + ".gif");
            JDeli.write(bi, OutputFormat.GIF, out);
        }
    }
} catch (PdfException | IOException e) {
    e.printStackTrace();
}  catch (Exception e) {
    e.printStackTrace();
}

convert.closePDFfile();

Javaでパスワードで保護されたPDFファイルをGIFに変換

ConvertPagesToImages convert = new ConvertPagesToImages("/path/file.pdf");
convert.setPassword("password");
try {
    if (convert.openPDFFile()) {
        for (int page = 1; page <= convert.getPageCount(); page++) {
            final BufferedImage bi = convert.getPageAsImage(page);
            final File out = new File("/path/to/output/" + page + ".gif");
            JDeli.write(bi, OutputFormat.GIF, out);
        }
    }
} catch (PdfException | IOException e) {
    e.printStackTrace();
}  catch (Exception e) {
    e.printStackTrace();
}

convert.closePDFfile();

アップスケールや、PDFからGIFへの変換をより複雑にコントロールしたい場合、ConvertPagesToHiResImagesクラスには多くの追加オプションがあります。

MENU
PAGE TOP