2

I want to convert an MS powerpoint (ppt) document to PDF.

I searched some jars like officetools.jar, but that requires purchase.

Is there any way I can convert it through itext and apache POI like we do for doc to PDF?

Joris Schellekens
  • 8,483
  • 2
  • 23
  • 54
user1037487
  • 29
  • 1
  • 1
  • 2

4 Answers4

6

I am using iText and apache poi:

Following is the list of imports-

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.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

and the method is..

    public void convertPPTToPDF(String sourcepath, String destinationPath, String fileType) throws Exception {
    FileInputStream inputStream = new FileInputStream(sourcepath);
    double zoom = 2;
    AffineTransform at = new AffineTransform();
    at.setToScale(zoom, zoom);
    Document pdfDocument = new Document();
    PdfWriter pdfWriter = PdfWriter.getInstance(pdfDocument, new FileOutputStream(destinationPath));
    PdfPTable table = new PdfPTable(1);
    pdfWriter.open();
    pdfDocument.open();
    Dimension pgsize = null;
    Image slideImage = null;
    BufferedImage img = null;
    if (fileType.equalsIgnoreCase(".ppt")) {
        SlideShow ppt = new SlideShow(inputStream);
        pgsize = ppt.getPageSize();
        Slide slide[] = ppt.getSlides();
        pdfDocument.setPageSize(new Rectangle((float) pgsize.getWidth(), (float) pgsize.getHeight()));
        pdfWriter.open();
        pdfDocument.open();
        for (int i = 0; i < slide.length; i++) {
            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[i].draw(graphics);
            graphics.getPaint();
            slideImage = Image.getInstance(img, null);
            table.addCell(new PdfPCell(slideImage, true));
        }
    }
    if (fileType.equalsIgnoreCase(".pptx")) {
        XMLSlideShow ppt = new XMLSlideShow(inputStream);
        pgsize = ppt.getPageSize();
        XSLFSlide slide[] = ppt.getSlides();
        pdfDocument.setPageSize(new Rectangle((float) pgsize.getWidth(), (float) pgsize.getHeight()));
        pdfWriter.open();
        pdfDocument.open();
        for (int i = 0; i < slide.length; i++) {
            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[i].draw(graphics);
            graphics.getPaint();
            slideImage = Image.getInstance(img, null);
            table.addCell(new PdfPCell(slideImage, true));
        }
    }
    pdfDocument.add(table);
    pdfDocument.close();
    pdfWriter.close();
    System.out.println("Powerpoint file converted to PDF successfully");
}
imdzeeshan
  • 1,098
  • 20
  • 26
Rohit patel
  • 101
  • 1
  • 4
1

I'd try using LibreOffice JAVA API to open PPT and to save it to PDF.

Boris Pavlović
  • 63,078
  • 28
  • 122
  • 148
1

Use JODConverter, but it requires OpenOffice.org.

amrfaissal
  • 1,060
  • 1
  • 7
  • 18
  • i want microsoft office files to pdf so can it be done through openoffice ? – user1037487 Dec 05 '11 at 07:14
  • OpenOffice does not support XML formatted documents(*.docx) but i think Novell distribut this version of OpenOffice that can open and save Microsoft Open-XML formatted documents. – amrfaissal Dec 05 '11 at 07:56
-2

Try using aspose. I personally , haven't used it before . Here's the Link

seeker
  • 6,841
  • 24
  • 64
  • 100