In this blog, I’ll explain the way to convert PDF, PPT and PPTX into images.

Sometimes, we get need to do this. The scenario could be like when we are uploading files in application -> storing them into database as image -> then showing those images to user.

In above case, First, we have to convert files (PDF, PPT or PPTX) into images.

So below are the code for all 3 to convert into images.

Convert PDF into images :
First of all we have to add dependency for this in build.sbt file:

"org.apache.pdfbox" % "pdfbox" % "1.8.2",
"org.apache.pdfbox" % "fontbox" % "1.8.2"

Code :

val file = new File(path + name)
val doc = PDDocument.load(new FileInputStream(file));
val pages = doc.getDocumentCatalog().getAllPages()
val listOfImage= for (i <- 0 until pages.size()) yield {
                     val page = pages.get(i)
                     val image = page.asInstanceOf[PDPage].convertToImage()
                     image
                  }

Convert PPT into images :
Add this dependency in build.sbt file :

"org.apache.poi" % "poi-scratchpad" % "3.9"

Code :

val file = new File(path + name)
  val is = new FileInputStream(file)
  val ppt = new SlideShow(is)
  is.close()
  val pgsize = ppt.getPageSize()
  val slide = ppt.getSlides();
  val listOfImage = for (i <- 0 until slide.length) yield {
    val img = new BufferedImage(pgsize.width, pgsize.height, 1)
    val graphics = img.createGraphics()
    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
    graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY)
    graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
      RenderingHints.VALUE_INTERPOLATION_BICUBIC)
    graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
      RenderingHints.VALUE_FRACTIONALMETRICS_ON)
    graphics.setColor(Color.white)
    graphics.clearRect(0, 0, pgsize.width, pgsize.height)
    graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height))

    // render
    slide(i).draw(graphics)
    img
  }

Convert PPTX into images :
Add this dependency in build.sbt file :

"org.apache.poi" % "poi-ooxml" % "3.9"

Code :

val file = new File(path + name)
  val is = new FileInputStream(file)
  val ppt = new XMLSlideShow(is)
  is.close()
  val pgsize = ppt.getPageSize()
  val slide = ppt.getSlides();
  val listOfImage = for (i <- 0 until slide.length) yield {
    val img = new BufferedImage(pgsize.width, pgsize.height, 1)
    val graphics = img.createGraphics()
    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
    graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY)
    graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
      RenderingHints.VALUE_INTERPOLATION_BICUBIC)
    graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
      RenderingHints.VALUE_FRACTIONALMETRICS_ON)
    graphics.setColor(Color.white)
    graphics.clearRect(0, 0, pgsize.width, pgsize.height)
    graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height))

    // render
    slide(i).draw(graphics)
    img
  }

3 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s