Convert Slide

You can convert presentation slides to any graphic image format that Java supports, such as PNG, BMP, JPEG, GIF, etc., by using Aspose.Slides API for Java. Use getThumbnail method of ISlide interface to convert slide to a BufferedImage object. Also, you can use ITiffOptions or IRenderingOptions interfaces to set additional options for conversion and convertible slide objects. These interfaces and their properties are described below in the specialized sections of the article.

Convert Slide to BufferedImage

The code example below shows how to convert the first slide of presentation to a PNG image.

Presentation pres = new Presentation("Presentation.pptx");
try {
    // Convert the first slide of the presentation to a Bitmap object
    BufferedImage bmp = pres.getSlides().get_Item(0).getThumbnail();
    {
        // Save the image in PNG format
        ImageIO.write(bmp, "PNG", new File("Slide_0.png"));
    }
} catch (Exception e) {  
} finally {
    if (pres != null) pres.dispose();
}

Convert Slide to Image with Custom Size

Sometimes you need to get an image of a slide of a certain size. The following example demonstrates this capability using one of the getThumbnail method overloads:

Presentation pres = new Presentation("Presentation.pptx");
try {
    // Convert the first slide of the presentation to a Bitmap with the specified size
    BufferedImage bmp = pres.getSlides().get_Item(0).getThumbnail(new Dimension(1820, 1040));
    {
        // Save the image in JPEG format
        ImageIO.write(bmp, "PNG", new File("Slide_0.jpg"));
    }
} catch (Exception e) {
} finally {
    if (pres != null) pres.dispose();
}

Convert Slide with Notes and Comments to Image

There are two interfaces ITiffOptions and IRenderingOptions, used to control the rendering of presentation slides to images. Both of these interfaces include the INotesCommentsLayoutingOptions interface, which can be used to include notes and comments of a slide in an exporting image. Using this interface, you can also control the position in which notes and comments will be displayed in the image. The following example demonstrates the usage of the INotesCommentsLayoutingOptions interface using the IRenderingOptions interface. An example of ITiffOptions interface usage will be provided below.

Presentation pres = new Presentation("PresentationNotesComments.pptx");
try {
    // Create rendering options
    IRenderingOptions options = new RenderingOptions();

    // Set the position of the notes on the page
    options.getNotesCommentsLayouting().setNotesPosition(NotesPositions.BottomTruncated);

    // Set the position of the comments on the page
    options.getNotesCommentsLayouting().setCommentsPosition(CommentsPositions.Right);

    // Set the width of the comment output area
    options.getNotesCommentsLayouting().setCommentsAreaWidth(500);

    // Set the color of comments area
    options.getNotesCommentsLayouting().setCommentsAreaColor(Color.LIGHT_GRAY);

    // Convert the first slide of the presentation to a Bitmap object
    BufferedImage bmp = pres.getSlides().get_Item(0).getThumbnail(options, 2f, 2f);

    // Save the image in GIF format
    ImageIO.write(bmp, "GIF", new File("Slide_Notes_Comments_0.gif"));
} catch (Exception e) {
} finally {
    if (pres != null) pres.dispose();
}

Convert Slide to Image using ITiffOptions Options

ITiffOptions provides a more complete control over the resulting image file. Using this interface, you can specify the size, resolution, color palette of the resulting image. Below is an example of using the ITiffOptions interface to get an image with 300dpi resolution and 2160x2880 size:

Presentation pres = new Presentation("PresentationNotesComments.pptx");
try {
    // Get a slide by its index
    ISlide slide = pres.getSlides().get_Item(0);

    // Create TiffOptions object
    TiffOptions options = new TiffOptions();
    options.setImageSize(new Dimension(2160, 2880));

    // Set font used in case source font is not found
    options.setDefaultRegularFont("Arial Black");

    // Set the position of the notes on the page
    options.getNotesCommentsLayouting().setNotesPosition(NotesPositions.BottomTruncated);

    // Set resolution
    options.setDpiX(300);
    options.setDpiY(300);

    // Convert slide to a Tiff image
    pres.save("Slide_Notes_Comments_0.tiff", SaveFormat.Tiff, options);
} catch (Exception e) {
} finally {
    if (pres != null) pres.dispose();
}

Convert Presentation to Set of Images

In some cases, it is necessary to convert the entire presentation into a set of images, the same as PowerPoint allows. The following example demonstrates this possibility:

Presentation pres = new Presentation("Presentation.pptx");
try {
    // Render presentation to images array slide by slide
    for (int i = 0 ; i < pres.getSlides().size(); i++)
    {
        // Control hidden slides (do not render hidden slides)
        if (pres.getSlides().get_Item(i).getHidden())
            continue;

        // Convert slide to a Bitmap object
        BufferedImage bmp = pres.getSlides().get_Item(i).getThumbnail(2f, 2f);
		
        // Create file name for an image
        String outputFilePath = outputDir + "Slide_" + i + ".jpg";

        // Save the image in PNG format
        ImageIO.write(bmp, "PNG", new File(outputFilePath));
    }
} catch (Exception e) {
} finally {
    if (pres != null) pres.dispose();
}