Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache poi slide show to pdf conversion

Is there any way to convert generated .ppt file using apache poi to .pdf file?

             OR

Any way to convert PPT file to PDF file using JAVA?

like image 338
RPB Avatar asked Dec 27 '22 13:12

RPB


1 Answers

Gagravarr, thanks you for your comment with following approach: PPT -> images -> PDF. It gave me clues for further solutions.

Recently, I've faced the same task: convert PPT reports into PDF reports using Java facilities. PPT reports are generated via Apache POI lib, and I intended to reuse ready created PPT structure.

I developed two solutions, each has its own advantages/disadvantages. And both of them using iText library with version 2.1.7.(which is free to use, and which is great)). Both of them do support Japanese symbols after additional enhancement.

1. Apache POI Slide -> image -> PDF

Demonstration code example:

package com.test.pdf;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
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.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;

public class PPTtoImageToPDFexample {


    public static void main(String[] args) throws IOException, DocumentException {

        //load any ppt file
        FileInputStream inputStream = new FileInputStream("d:/temp/initialPPT.ppt");
        SlideShow ppt = new SlideShow(inputStream);
        inputStream.close();
        Dimension pgsize = ppt.getPageSize();

        //take first slide and save it as an image
        Slide slide = ppt.getSlides()[0];
        BufferedImage img = new BufferedImage(pgsize.width, pgsize.height,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = img.createGraphics();
        graphics.setPaint(Color.white);
        graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width,
                pgsize.height));
        slide.draw(graphics);
        FileOutputStream out = new FileOutputStream("d:/temp/slideImage.png");
        javax.imageio.ImageIO.write(img, "png", out);
        out.close();


        //get saved slide-image and save it into pdf
        Image slideImage = Image.getInstance("d:/temp/slideImage.png");
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream("d:/temp/PPTtoImageTest.pdf"));
        document.setPageSize(new Rectangle(slideImage.getWidth(), slideImage.getHeight()));
        document.open();
        slideImage.setAbsolutePosition(0, 0);
        document.add(slideImage);
        document.close();

    }
}

2. This approach works on-fly: take Apache POI Slide -> get awt.Graphics2 instance from it -> pass this interface to the iText draw engine.

Demonstration code example:

package com.test.pdf;

import java.awt.Dimension;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfGraphics2D;
import com.lowagie.text.pdf.PdfWriter;

public class PPTtoPDFdirectly {



    public static void main(String[] args) throws IOException, DocumentException {

        //load any ppt file
        FileInputStream inputStream = new FileInputStream("d:/temp/initialPPT.ppt");
        SlideShow ppt = new SlideShow(inputStream);
        inputStream.close();
        Dimension pgsize = ppt.getPageSize();


        //take first slide and draw it directly into PDF via awt.Graphics2D interface.
        Slide slide = ppt.getSlides()[0];

        Document document = new Document();
        PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("d:/temp/PPTtoPDF.pdf"));
        document.setPageSize(new Rectangle((float)pgsize.getWidth(), (float) pgsize.getHeight()));
        document.open();

        PdfGraphics2D graphics = (PdfGraphics2D) pdfWriter.getDirectContent().createGraphics((float)pgsize.getWidth(), (float)pgsize.getHeight());
        slide.draw(graphics);
        graphics.dispose();

        document.close();

    }
}
like image 91
Alex Avatar answered Dec 30 '22 02:12

Alex