如何在Java中将ppt文件转换为pdf文件?

时间:2011-12-02 09:59:04

标签: java pdf powerpoint itext apache-poi

我想将MS powerpoint(ppt)文档转换为PDF。

我搜索了一些像officetools.jar这样的罐子,但这需要购买。

我有什么方法可以通过itextapache POI转换它,就像我们为doc转换为PDF一样?

4 个答案:

答案 0 :(得分:5)

我正在使用iText和apache poi:

以下是导入列表 -

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;

,方法是..

    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");
}

答案 1 :(得分:1)

我尝试使用LibreOffice JAVA API打开PPT并将其保存为PDF。

答案 2 :(得分:1)

使用JODConverter,但需要OpenOffice.org。

答案 3 :(得分:-1)

尝试使用aspose。我个人,以前没用过它。 这是Link