Convert Presentation slides to multiple images in Java

Using the java api version 20.6 can you please provide sample code for how to convert a PPTX into multiple images (one image per slide)? All the docs and examples I can find only do one image at a time, which means re-reading the input source each time. The ConvertedPageStream thing isn’t very clear, and when I try to use it I get “Specified method is not supported.”

1 Like

@rstauner

We are investigating this scenario. Your investigation ticket ID is CONVERSIONJAVA-1033. You’ll be notified in case of any update.

Thanks. We’ve been doing this for years (since v 16) using an OutputHandler that defined saveFile (which was called once per slide) but i can’t see how to do it with the new v 20 api.

@rstauner

We’re investigating it. We’ll surely notify you as there’s any update.

@rstauner

Please have a look at this code:

public class ConvertToJpg {
    public static void run() {
        String outputFolder = Constants.getOutputDirectoryPath(null);
        String outputFileTemplate = new File(outputFolder, "converted-page-%d.jpg").getPath();

        Converter converter = new Converter(Constants.SAMPLE_PDF);

        ImageConvertOptions options = new ImageConvertOptions();
        options.setFormat(ImageFileType.Jpg);
        options.setPagesCount(1);

        int pagesCount = converter.getDocumentInfo().getPagesCount();

        for (int i = 1; i <= pagesCount; i++) {
            converter = new Converter(Constants.SAMPLE_PDF); //workaround, will be fixed
            options.setPageNumber(i);
            try (FileOutputStream getPageStream = new FileOutputStream(String.format(outputFileTemplate, i))) {
                converter.convert(getPageStream, options);
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }

        System.out.print("\nConversion to jpg completed successfully. \nCheck output in " + outputFolder);
    }
}

Let us know if it helps.

Thanks, that’s precisely what I had already tried that (it was the only way i was able to get it to work). It does work, but for large input files it’s really inefficient to re-read it everytime. If that’s my only option I’m not going to upgrade.

@rstauner

We’ll further see if this could be improved.

@rstauner

Instead of reloading the same file we can load it once to byte array and reuse this byte array via ByteArrayInputStream to convert every page.

  private static void convertToJpg() throws IOException {
        byte[] fileData = Files.readAllBytes(new File("Input.pptx").toPath());
        int pagesCount;
        try (ByteArrayInputStream inputStream = new ByteArrayInputStream(fileData)) {
            Converter converter = new Converter(inputStream);
            pagesCount = converter.getDocumentInfo().getPagesCount();
        }
        for (int i = 1; i <= pagesCount; i++) {
            try (ByteArrayInputStream inputStream = new ByteArrayInputStream(fileData)) {
                Converter converter = new Converter(inputStream);
                ImageConvertOptions options = new ImageConvertOptions();
                options.setFormat(ImageFileType.Jpg);
                options.setPagesCount(1);
                options.setPageNumber(i);
                converter.convert("output" + i + ".jpg", options);
            }
        }
    }