开发者

Convert file .pptx to .ppt using Java

开发者 https://www.devze.com 2023-03-10 05:35 出处:网络
I was wondering if someone knows a way to convert .pptx to开发者_如何学Go .ppt progamatically using Java?Use Apache POI.You can use openoffice for conversion. You have to configure eclipse/netbeans pr

I was wondering if someone knows a way to convert .pptx to开发者_如何学Go .ppt progamatically using Java?


Use Apache POI.


You can use openoffice for conversion. You have to configure eclipse/netbeans properly. You need jodconverter plugin, too. oh, and remember to open OO in listening mode

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 is the only library I've seen that understands pptx. It's not free but it would probably have the ability to do the conversion. Apache POI is a free ppt Java library but last I checked it didn't support pptx.

Update: here's how I extracted images using Aspose. Once you have png files, you should be able to build a PDF using other tools. I needed explicitly sized images - you may be able to just get it as the native size:

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

精彩评论

暂无评论...
验证码 换一张
取 消