简体   繁体   中英

Convert file .pptx to .ppt using Java

I was wondering if someone knows a way to convert.pptx to.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));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM