我想知道是否有一种使用Java程序自动将.pptx转换为.ppt的方法?
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();
}
}
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));
}