< 戻る
印刷

JavaでPDFを画像に変換する

Javaコードまたはコマンドラインを使用して、PDFページを画像に変換する方法を示す簡単なサンプルコードです。サンプルコードをコピー&ペーストするだけでご利用いただけます。

JPedalは、PDFファイルまたはPDFファイルのディレクトリをBMPに変換するためのいくつかのメソッドを提供します。JavaサンプルはConvertPagesToImagesBmpEncoderOptions クラスを使用しています。

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

				
					java -jar <a href="https://www.intwk.co.jp/jpedal/" title="PDFドキュメントを扱う必要のあるJava開発者のためのPDFライブラリです。このライブラリは、PDFファイルを扱う際の一般的な問題を解決します。" hreflang="ja" onover-preload="1">jpedal</a>.jar --convert "inputFileOrDir" "outputDir" bmp
				
			
JavaでPDFをBMPに変換する便利なスタティックメソッド
				
					<a href="https://support.idrsolutions.com/jpedal/tutorials/convert-images/convert-pdf-to-bmp" target="_blank" title="JPedal provides several methods to allow easy conversion of a PDF file or directory of PDF files into BMP." hreflang="en-US">ConvertPagesToImages</a>.writeAllPagesAsImagesToDir("inputFileOrDir", "outputDir" , "bmp", 1.33f);

				
			
JavaでPDFからBMPに変換し、画像出力を制御する
				
					ConvertPagesToImages convert = new ConvertPagesToImages("/path/to/file.pdf");
try {
    if (convert.openPDFFile()) {
        for(int page = 1; page 
				
			
出力のページ範囲を制御しながら、JavaでPDFからBMPに変換する
				
					<a href="https://www.intwk.co.jp/jpedal-portal/convert-pdf-to-bufferedimage/" title='PDFをBufferedImageに変換する簡単な例JPedalは、PDFファイルやPDFファイルのディレクトリからJava BufferedImagesへの簡単な変換を提供します。これはJPedal ConvertPagesToImagesクラスを使用します。コード例 - ファイルパスを使ってPDFファイルを画像に変換 ConvertPagesToImages convert = new ConvertPagesToImages("/pat' hreflang="ja" onover-preload="1">ConvertPagesToImages</a> convert = new ConvertPagesToImages("/path/to/file.pdf");
// setPageRange gives you the ability to chose the pages you'd like using '-' or ':' for range 
// and ',' to move to the next range or you can simply put null for all the pages
convert.setPageRange(new PageRanges("1-5,8:10,15")); 
// Above will give us pages 1 to 5(inclusive),8 to 10(inclusive) and 15
try {
    if (convert.openPDFFile()) {
        convert.getPageRange().forEachRemaining(page -&gt; {
            try {
                final BufferedImage bi = convert.getPageAsImage(page);
                final File out = new File("/path/to/output/" + page + ".bmp");
                JDeli.write(bi, options, out);  
            } catch (Exception e) {
                e.printStackTrace();
            }
       });  
    }
} catch (PdfException e) {
    e.printStackTrace();
}                  

convert.closePDFfile();
				
			
JavaでPDFからBMPのサムネイルに変換し、出力画像の寸法を制御する
				
					ConvertPagesToImages.
writeAllPagesAsImagesToDir("inputFileOrDir", "outputDir" , "bmp", new int[]{width,height});
				
			

または

				
					ConvertPagesToImages convert = new ConvertPagesToImages("/<a href="https://pub.dev/packages/path" target="_blank" title="A string-based path manipulation library. All of the path operations you know and love, with solid support for Windows, POSIX (Linux and Mac OS X), and the web." hreflang="en-us">path</a>/to/file.pdf");
//fit with aspect ratio preserved (width will be 300 or height will be 400)
convert.setFitToSize(new int[]{300,400}); 
try {
    if (convert.openPDFFile()) {
        for (int page = 1; page 
				
			
JavaでPDFからBMPのサムネイルに変換し、拡大縮小を制御できるようにする
				
					ConvertPagesToImages convert = new ConvertPagesToImages("/path/to/file.pdf");
convert.setPageScaling(1.33f); //which gives same size as Acrobat at 100%
try {
    if (convert.openPDFFile()) {
        for (int page = 1; page 
				
			
パスワードで保護されたPDFファイルをJavaでPDFからBMPに変換する
				
					ConvertPagesToImages convert = new ConvertPagesToImages("/path/file.pdf");
convert.setPassword("password"); 
try {
    if (convert.openPDFFile()) {
        for (int page = 1; page 
				
			

PDFからBMPへの変換でアップスケーリングやより複雑な制御をしたい場合、ConvertPagesToHiResImagesクラスには多くの追加オプションがあります。

MENU
PAGE TOP