Unable to render images to a PPTX slideshow using Apache POI

48 views Asked by At

I have this code that reads a .pptx template file to fetch placeholder mappings and then subsequently replace these placeholders with actual data. It uses Apache POI in Scala.

Here is my code:

def replaceTags(slide: XSLFSlide, data: Data, slideShow: XMLSlideShow): Unit = {
  slide.getShapes.asScala
    .foreach {
      case textShape: XSLFTextShape       => // shape that can hold text.
        val text = textShape.getText
        val updatedText = replaceTags(text, data)

        textShape.setText(updatedText)

        val updatedAnchor = getPlaceholderAnchor(textShape)
        textShape.setAnchor(updatedAnchor)

        textShape

      case pictureShape: XSLFPictureShape =>
        val updatedPictureOpt = getPictureData(data.PIM_MAIN_IMAGE, slideShow)

        updatedPictureOpt.foreach { updatedPicture =>
          slide.createPicture(updatedPicture)

          pictureShape.setAnchor(getPlaceholderAnchor(pictureShape))
        }
        
      case shape                          => shape
    }
}


private def getPictureData(filePathOpt: Option[String], slideShow: XMLSlideShow): Option[XSLFPictureData] = {
  filePathOpt.flatMap { filePath =>
    val imgBytes    = IOUtils.toByteArray(Files.newInputStream(Path.of(filePath)))
    val pictureType = PictureData.PictureType.valueOf(FileNameUtils.getExtension(filePath))

    slideShow.addPicture(imgBytes, pictureType).some
  }
}

Unfortunately, the images are not being rendered in the slides - instead, only the image's file path is being rendered.

Any ideas where I may be going wrong and hopefully how can I address this?

Suggestions will be greatly appreciated.

0

There are 0 answers