使用Java将文件.pptx转换为.ppt

时间:2011-06-01 18:33:25

标签: java powerpoint

我想知道是否有人知道如何使用Java以编程方式将.pptx转换为.ppt?

3 个答案:

答案 0 :(得分:2)

使用Apache POI

答案 1 :(得分:2)

您可以使用openoffice进行转换。你必须配置eclipse / netbeans properly。你也需要jodconverter插件。 哦,记得在listening mode

中打开OO
package openofficeconv;
import java.io.File;
import java.net.ConnectException;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.*;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

public class PowerpointConverter{

public static void main(String[] args) {

    File inputFile = new File("j.pptx");
    File outputFile = new File("j.pdf");

    // connect to an OpenOffice.org instance running on port 8100
    OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
    try {
        connection.connect();
    } catch (ConnectException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // convert
    DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
    converter.convert(inputFile, outputFile);

    // close the connection
    connection.disconnect();
}
}

答案 2 :(得分:0)

Aspose Slides是我见过的唯一了解pptx的库。它不是免费的,但它可能有能力进行转换。 Apache POI是一个免费的ppt Java库,但最后我检查它不支持pptx。

更新:这是我使用Aspose提取图像的方式。拥有png文件后,您应该能够使用其他工具构建PDF。我需要明确大小的图像 - 您可以将其作为原始大小:

Dimension small = new Dimension(160, 120);
Dimension medium = new Dimension(200,150);
Dimension large = new Dimension(400,300);

for (Slide slide : presentation.getSlides()) {
  String path = FileService.getUploadPath() + slide.getPath();
  com.aspose.slides.Slide pptSlide = ppt.getSlideByPosition(slide.getSequence());
  ImageIO.write(pptSlide.getThumbnail(1, 1), "png", new File(path));

  path = FileService.getUploadPath() + slide.getSmallPath();
  ImageIO.write(pptSlide.getThumbnail(small), "png", new File(path));

  path = FileService.getUploadPath() + slide.getMediumPath();
  ImageIO.write(pptSlide.getThumbnail(medium), "png", new File(path));

  path = FileService.getUploadPath() + slide.getLargePath();
  ImageIO.write(pptSlide.getThumbnail(large), "png", new File(path));
}