Convert file .pptx to .ppt using Java

2020-07-23 06:34发布

问题:

I was wondering if someone knows a way to convert .pptx to .ppt progamatically using Java?

回答1:

Use Apache POI.



回答2:

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


回答3:

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