0

I am an SQL/ETL(PowerCenter)/bash/Python developer with a very little experience in Java. I have a task - I need to take a .pptx template, customize it and convert to a .pdf file. I've decided to start from the second step, so I took this as an example. I've got latest versions of libraries (iText7 and POI4), so I had to modify this code in order to compile it. I was able to find moved packages in an Import part but then I stuck here:

slideImage = Image.getInstance(img, null);

My new libraries say that getInstance is not supported anymore (cannot find symbol). I'm trying to skip this step and use an analogue of

table.addCell(new PdfPCell(slideImage, true));

which I've changed to

table.addCell(new Cell(img, true));

to add this bufferedImage directly to a cell, it throws conversion errors, like "BufferedImage cannot be converted to int). How can I convert BufferedImage to Image? I read that the 1st is a child of the 2nd, so there's no need to convert it, but it doesn't work. Below I'm providing the code adjusted by me. Thank you in advance!


import java.io.FileOutputStream;
import java.io.*;
import java.io.IOException;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hslf.record.Slide;
import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;

import com.itextpdf.layout.Document;

import com.itextpdf.layout.element.Image;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Table;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.geom.PageSize;

public void createPdf(String inFileName, String outFileName)
    throws IOException 
{
    FileInputStream inputStream = new FileInputStream(inFileName);
    double zoom = 2;
    AffineTransform at = new AffineTransform();
    at.setToScale(zoom, zoom);

    Table table = new Table(1);
    Dimension pgsize = null;
    Image slideImage = null;
    BufferedImage img = null;

    XMLSlideShow ppt = new XMLSlideShow(inputStream);
    pgsize = ppt.getPageSize();

//  PDF part
//  Initialize PDF writer
    PdfWriter writer = new PdfWriter(outFileName);
//  Initialize PDF document
    PdfDocument pdf = new PdfDocument(writer);
//  Initialize document
    Rectangle srcPageSize = new Rectangle((float) pgsize.getWidth(), (float) pgsize.getHeight());
    Document doc = new Document(pdf, new PageSize(srcPageSize));

    List<XSLFSlide> slides = ppt.getSlides();
//  writer.open();
//  pdfDocument.open();
    for (XSLFSlide slide : ppt.getSlides()) {
        img = new BufferedImage((int) Math.ceil(pgsize.width * zoom), (int) Math.ceil(pgsize.height * zoom), BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = img.createGraphics();
        graphics.setTransform(at);

        graphics.setPaint(Color.white);
        graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
        slide.draw(graphics);
        graphics.getPaint();

//  Original start
//      slideImage = Image.getInstance(img, null);
//      table.addCell(new PdfPCell(slideImage, true));
//  Original end


        table.addCell(new Cell(img, true));    
    }
//  pdfDocument.add(table);
//  pdfDocument.close();
//  pdfWriter.close();
    System.out.println("Powerpoint file converted to PDF successfully");

//  catch (IOException e) 
//  {
//      System.err.println("FileStreamsReadnWrite: " + e);
//  }
}

1 Answers1

1

You may try the following , which uses the current API :

// you need to convert the BufferedImage to a byte array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, "png", baos);

ImageData image = ImageDataFactory.create(baos.toByteArray());

table.addCell(new Image(image));

There are other interesting methods like ImageDataFactory.create(String filename) .

Arnaud
  • 17,229
  • 3
  • 31
  • 44
  • Hi Arnaud! Thank you for an answer, i got the following compilation time error on the "Image image = ImageDataFactory.create(baos.toByteArray());" line - "incompatible types: ImageData cannot be converted to Image" – Serge Larionoff Nov 09 '18 at 12:44
  • Super, at least now it compiles successfully! After compilation I've started an execution and I got another error " [ERROR] Failed to load class [com.itextpdf.layout.element.Table] : [com.itextpdf.layout.element.Table]. [ERROR] java.lang.NoClassDefFoundError: com/itextpdf/layout/element/Table" I know that I do have this line in an import section. What could went wrong this time? Does this mean that a file with that class is not accessible on server? But I see that the folder with .jar files is listed in a $CLASSPATH. – Serge Larionoff Nov 09 '18 at 13:41
  • It seems that the iText jar that contains this class is not in the classpath at runtime. – Arnaud Nov 09 '18 at 13:43
  • See : https://stackoverflow.com/questions/17973970/how-to-solve-java-lang-noclassdeffounderror – Arnaud Nov 09 '18 at 14:02