Javaexamples-extract-image-from-pdf

提供:Dev Guides
移動先:案内検索

Javaの例-PDFから画像を抽出する

問題の説明

Javaを使用してPDFから画像を抽出する方法。

溶液

以下は、Javaを使用してPDFから画像を抽出するプログラムです。

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

public class ExtractImageFromPdf {
   public static void main(String args[]) throws Exception {

     //Loading an existing PDF document
      File file = new File("C:/pdfBox/ExtractImage_IP.pdf");
      PDDocument document = PDDocument.load(file);

     //Instantiating the PDFRenderer class
      PDFRenderer renderer = new PDFRenderer(document);

     //Rendering an image from the PDF document
      BufferedImage image = renderer.renderImage(0);

     //Writing the image to a file
      ImageIO.write(image, "JPEG", new File("C:/pdfBox/ExtractImage_OP.png"));
      System.out.println("Image created");

     //Closing the document
      document.close();
   }
}

入力

入力の抽出

出力

出力の抽出