home|blog|HOW TO CONVERT A PDF TO AN IMAGE

How to convert a PDF to an image

To make a preview image of a PDF document I do use PDFBox that is an Apache library made to work with PDF.

You can install the library manually or use the Maven repository:

<dependency>
	<groupId>org.apache.pdfbox<groupId>
	<artifactId>pdfbox</artifactId>
	<version>1.6.0</version>
<dependency>

To convert the PDF, here it is the code:

PDDocument doc = PDDocument.load("myFile.pdf");
PDPage firstPage = (PDPage) doc.getDocumentCatalog().getAllPages().get(0);
BufferedImage bufferedImage = firstPage.convertToImage();

You can even load the PDF document by a Stream or a Url. You have the first page preview of your PDF file in the BufferdImage variable. Then you can save it into the preferred extension.

share:

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*