Wednesday, July 21, 2010

Generate Book Cover from Pdf Documents using PdfBox

PdfBox is a very powerful tool to deal with Pdf documents. Today I will show you how to generate image from pdf page and re-size it to use as book cover. The book cover usually has two images, one for thumbnail, 180*236, the other is large one, 500*656, as you see from some book store websites.



There is a "PdfImageWriter" class shipped with PdfBox. However, this class is not flexible. It can only generate a series of image with fixed size and auto-increased file name(such as image1.jpg, image2.jpg). Therefore, I create a "FlexiablePdfImageWriter" class to generate and write image with defined weight and height.

public class FlexiablePdfImageWriter  {

 private PDDocument document;
 // default setting for image params
 private int resolution = 96;
 private int imageType = BufferedImage.TYPE_INT_RGB;
 private String imageFormat = "jpg";

 public FlexiablePdfImageWriter(PDDocument document) {
  this.document = document;
 }

/**
 * Generate image for given page number, begin at 1
 * @param pageNo page number
 * @return image
 * @throws IOException
 */
 public BufferedImage getOriginalImageForSinglePage(int pageNo)
   throws IOException {
  List pages = document.getDocumentCatalog().getAllPages();

  PDPage page = (PDPage) pages.get(pageNo - 1);
  BufferedImage image = page.convertToImage(imageType, resolution);

  return image;

 }

 /**
  * resize image with width, height
  * @param orginalImage
  * @param width
  * @param height
  * @return resiced image
  */
 public BufferedImage resizeImage(BufferedImage orginalImage, int width, int height)
   {

  BufferedImage resizedImage = new BufferedImage(width, height, imageType);
  resizedImage.getGraphics().drawImage(orginalImage, 0, 0, width, height, null);

  return resizedImage;

 }
 /**
  * resize image by percentage
  * @param orginalImage
  * @param percentage
  * @return scaled image
  */
 public BufferedImage scaleImage(BufferedImage orginalImage, int percentage)
{
  int width= orginalImage.getWidth()*percentage/100;
  int height= orginalImage.getHeight()*percentage/100;

  return this.resizeImage(orginalImage, width, height);

}
 /**
  * write image to file
  * @param image
  * @param path
  * @return
  * @throws IOException
  */
 public boolean writeImagte(BufferedImage image, String path)
   throws IOException {
  File outfile = new File(path);
  ImageIO.write(image, imageFormat, outfile);
  return true;
 }
 

 public int getResolution() {
  return resolution;
 }

 public void setResolution(int resolution) {
  this.resolution = resolution;
 }

 public int getImageType() {
  return imageType;
 }

 public void setImageType(int imageType) {
  this.imageType = imageType;
 }

 public String getImageFormat() {
  return imageFormat;
 }

 public void setImageFormat(String imageFormat) {
  this.imageFormat = imageFormat;
 }

}


How to use FlexiablePdfImageWriter:

@Test
 public void testGenerateImage() throws Exception {
  
  //prepare Pdf document
  File file = new File("Mastering Regular Expressions.pdf");
  PDDocument document = PDDocument.load(file);
  
  
  FlexiablePdfImageWriter imageWriter = new FlexiablePdfImageWriter(document);

    //you can change the default setting  uses setter
  //imageWriter.setImageFormat("png");
  //imageWriter.setResolution(64);

  //orginal size, PdfBox calculated by it's page size, margin and others.
  BufferedImage orginalImage = imageWriter.getOriginalImageForSinglePage(1);
  
  //generate thumbnail, size:180*236
  BufferedImage thumbnail = imageWriter.resizeImage(orginalImage, 180, 236);
  
  imageWriter.writeImagte(thumbnail, "thumbnail.jpg");
  
  //generate large cover, size:500*656
  BufferedImage large = imageWriter.resizeImage(orginalImage, 500, 656);
  
  imageWriter.writeImagte(large, "large.jpg");

//scale the orginal image, make it half size,  50% 
  BufferedImage half = imageWriter.scaleImage(orginalImage, 50);
  
  imageWriter.writeImagte(large, "half.jpg");
 }

0 comments: