How to convert PDF, PPT and PPTX into images in Scala


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
  }

About Rishi Khandelwal

Sr. Software Consultant having more than 6 years industry experience. He has working experience in various technologies such as Scala, Java, Play, Akka, Spark, Hive, Cassandra, Akka-http, ElasticSearch, Backbone.js, html5, javascript, Less, Amazon EC2, WebRTC, SBT
This entry was posted in Scala. Bookmark the permalink.

3 Responses to How to convert PDF, PPT and PPTX into images in Scala

  1. Charles says:

    Hey … is it possible to repeat this code but in php so I could convert pptx into images? thks

  2. Sorry Charles. I have no idea about PHP.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s