Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert PDF to thumbnail image in Java [closed]

Tags:

Can anybody suggest me a free Java library that can convert a PDF and create a thumbnail image (PNG) from the first page.

Thanks.

like image 405
Nims Avatar asked Feb 08 '11 05:02

Nims


People also ask

How do I save a PDF as a thumbnail image?

Open Acrobat or Acrobat Reader. On the Edit menu, choose Preferences. In the Preferences dialog box, choose General in the Categories list, and then select the Enable PDF thumbnail previews in Windows Explorer check box.

How do I make a PDF thumbnail in Wordpress?

You will need to visit Media » Library page and switch to the list view by clicking on the list view button. After that take your mouse over to a PDF file, and you will be able to see 'Force regenerate thumbnails' link. Clicking on the link will regenerate thumbnails for that particular file.


2 Answers

You could try pdf-renderer it is a pure java solution. The following Code creates an image of the first page.

File pdfFile = new File("/path/to/pdf.pdf"); RandomAccessFile raf = new RandomAccessFile(pdfFile, "r"); FileChannel channel = raf.getChannel(); ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); PDFFile pdf = new PDFFile(buf); PDFPage page = pdf.getPage(0);  // create the image Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(),                                  (int) page.getBBox().getHeight()); BufferedImage bufferedImage = new BufferedImage(rect.width, rect.height,                                   BufferedImage.TYPE_INT_RGB);  Image image = page.getImage(rect.width, rect.height,    // width & height                             rect,                       // clip rect                             null,                       // null for the ImageObserver                             true,                       // fill background with white                             true                        // block until drawing is done ); Graphics2D bufImageGraphics = bufferedImage.createGraphics(); bufImageGraphics.drawImage(image, 0, 0, null); ImageIO.write(bufferedImage, format, new File( "/path/to/image.jpg" )); 
like image 199
sdorra Avatar answered Sep 28 '22 03:09

sdorra


Excellent sdorra, thanks for your input. I have re-work your example in order to convert all the pages from the pdf.

Hope that will help some of you guys.

import java.awt.Graphics2D; import java.awt.Image; import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel;  import javax.imageio.ImageIO;  import com.sun.pdfview.PDFFile; import com.sun.pdfview.PDFPage;  public class Main {      public static void main(String[] args) throws IOException {         File pdfFile = new File("c:\\YOURPDF.pdf");         RandomAccessFile raf = new RandomAccessFile(pdfFile, "r");         FileChannel channel = raf.getChannel();         ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());         PDFFile pdf = new PDFFile(buf);          for (int i=0; i<pdf.getNumPages(); i++) {             createImage(pdf.getPage(i), "c:\\PICTURE_" + i + ".jpg");         }     }      public static void createImage(PDFPage page, String destination) throws IOException{         Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(),                 (int) page.getBBox().getHeight());         BufferedImage bufferedImage = new BufferedImage(rect.width, rect.height,                          BufferedImage.TYPE_INT_RGB);          Image image = page.getImage(rect.width, rect.height,    // width & height                    rect,                       // clip rect                    null,                       // null for the ImageObserver                    true,                       // fill background with white                    true                        // block until drawing is done         );         Graphics2D bufImageGraphics = bufferedImage.createGraphics();         bufImageGraphics.drawImage(image, 0, 0, null);         ImageIO.write(bufferedImage, "JPG", new File( destination ));     }  } 

You can download the library from pdf-renderer-1.0.5.jar

The Source for Java Technology Collaboration website

like image 30
Dax Avatar answered Sep 28 '22 01:09

Dax