PDFBox Extracting ImageIn this section, we will learn how to extract image from the existing PDF document. The PDFBox library provides a PDFRender class which renders a PDF document into an AWT BufferedImage. Follow the steps below to extract an image from the existing PDF document- Load Existing PDF DocumentWe can load the existing PDF document by using the static load () method. This method accepts a file object as a parameter. We can also invoke it using the class name PDDocument of the PDFBox. Instantiate the PDFRender classPDFRenderer class renders a PDF document into an AWT BufferedImage. The instance of this class needs a document object as its parameter. This can be shown in the following code. Render ImageThe renderImage() method of the Renderer class can be used to render the image in a particular page. This method need to pass the index of the page, where we have the image that is to be rendered. Writing the Image to a FileWe can write the rendered image to a file using the write () method. In this method, we need to pass three parameters -
This can be shown in the following code: Close DocumentAfter completing the task, we need to close the PDDocument class object by using the close () method. Example-This is a PDF document which we are going to extract its page as an image by using PDFBox library of a Java program. Java ProgramOutput: After successful execution, the above program shows the following output. Now for verification, open the image as shown below-
Next TopicPDFBox Get Location and Image Size
|