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

3
我想知道是否有一种使用Java程序自动将.pptx转换为.ppt的方法?

请使用注释来询问对答案的澄清。 - Tim Post
3个回答

2
你可以使用OpenOffice进行转换。你需要正确配置Eclipse/NetBeans,具体可参考这里。同时,你还需要安装jodconverter插件。另外,请记得以监听模式打开OpenOffice。
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));
}

POI通过POI-OpenXML4J支持PPTX。我没有使用过它,但Aspose看起来也很可靠。 - Matt Ball
好的,我正在查看POI链接中的这行代码:HSLF是POI项目对Powerpoint '97(-2007)文件格式的纯Java实现。它不支持新的PowerPoint 2007 .pptx文件格式,因为它不是基于OLE2的。 - Brian Deterling
我尝试使用Aspose Slide,但是遇到了一些问题。它可以将.pptx转换为pdf,但在pdf上,我没有图片。我的目标是将.pptx转换为pdf。我有一个将.ppt转换为pdf的解决方案。 - Ophelie
更新了如何获取图片的示例。 - Brian Deterling
谢谢,现在我已经使用Aspose得到了图片。但是我仍然有另一个与图片相关的问题。在另一个演示文稿中,我使用PowerPointOffice裁剪了图片。当我生成pdf文件时,图片是全尺寸的。 有人知道我如何获得正确的尺寸,也就是我裁剪的那个尺寸吗? - Ophelie
你尝试过最新版本的Aspose.Slides for Java吗?如果没有,请下载并在您的端上尝试。如果您仍然发现相同的问题或它不能完全满足您的要求,那么请在Aspose.Slides支持论坛中发布一个查询,这样我们的支持/开发团队就能够建议解决方案或添加新功能以满足您的情况。声明:我是Aspose的开发者推广员。 - Shahzad Latif

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接