Convert Presentation Slides to Images in Node.js via .NET
Overview
Aspose.Slides for Node.js via .NET renders slides from PowerPoint and OpenDocument presentations as images, for example to show slide previews on a web page. This article shows two ways to choose the image size: a scale factor relative to the slide size, and an exact size in pixels. Both examples save PNG files.
The examples expect a presentation named sample.pptx in the project folder that you set up in Installation. Any PowerPoint presentation will do. Save each example as a .js file in the project folder and run it from that folder with node.
Note
Aspose.Slides for Node.js via .NET has no API reference of its own. It mirrors the Aspose.Slides for .NET API with camelCase names, so the API links in this article lead to the matching classes and members in the Aspose.Slides for .NET API reference.To convert a slide to an image, follow these steps:
- Open the presentation with the Presentation constructor.
- Get a slide from the slides collection with
get(index). Indexes start at 0. - Render the slide with
getImageWithScaleorgetImageWithImageSize. In the .NET API reference, both are overloads of Slide.GetImage. They return an image object that corresponds to IImage. - Save the image with its save method and an ImageFormat value, and then call its
disposemethod.
Convert Every Slide to a PNG Image
getImageWithScale takes a horizontal and a vertical scale factor. At a scale of 1, one point of the slide becomes one pixel of the image. The following example renders every slide at a scale of 2:
const { Presentation, ImageFormat } = require("aspose.slides.via.net");
// A scale of 1 renders one pixel per point; 2 doubles the width and the height.
const scaleX = 2;
const scaleY = scaleX;
const presentation = new Presentation("sample.pptx");
try {
const slideCount = presentation.slides.count;
for (let index = 0; index < slideCount; index++) {
const slide = presentation.slides.get(index);
const image = slide.getImageWithScale(scaleX, scaleY);
try {
image.save(`slide_${index + 1}.png`, ImageFormat.Png);
} finally {
image.dispose();
}
}
console.log(`Saved ${slideCount} images`);
} finally {
presentation.dispose();
}
The script writes one file per slide, slide_1.png, slide_2.png, and so on, numbered from 1. For a 16:9 presentation with slides of 960 × 540 points, each image is 1920 × 1080 pixels. Hidden slides are rendered too; to skip them, check the slide’s hidden property. Each image is disposed in its own finally block, which releases it before the next slide is rendered. Without a license, the images also show an evaluation watermark; see Licensing.
Convert a Slide to an Image of a Given Size
getImageWithImageSize takes an object with width and height in pixels. The following example renders the first slide 1280 pixels wide and calculates the height from the slide size, so that the image keeps the slide’s aspect ratio:
const { Presentation, ImageFormat } = require("aspose.slides.via.net");
const imageWidth = 1280;
const presentation = new Presentation("sample.pptx");
try {
const slideSize = presentation.slideSize.size;
const imageHeight = Math.round(imageWidth * slideSize.height / slideSize.width);
const slide = presentation.slides.get(0);
const image = slide.getImageWithImageSize({ width: imageWidth, height: imageHeight });
try {
image.save("slide_1_1280px.png", ImageFormat.Png);
} finally {
image.dispose();
}
console.log(`Saved a ${imageWidth} x ${imageHeight} image`);
} finally {
presentation.dispose();
}
The slideSize.size property returns the slide width and height in points. For a 16:9 presentation, the script prints Saved a 1280 x 720 image and writes slide_1_1280px.png; for a 4:3 presentation, the image is 1280 × 960 pixels.
FAQ
Why is the image from getImage without arguments so small?
Without arguments, getImage renders the slide at 20% of its size in points, so a 960 × 540 point slide becomes a 192 × 108 pixel image. Use getImageWithScale or getImageWithImageSize to choose the size.
How do I save JPEG or other image formats?
Pass another ImageFormat value to the image’s save method, for example image.save("slide_1.jpg", ImageFormat.Jpeg). The format comes from the ImageFormat value, not from the file extension, so keep the two consistent.
Why does the text in the images look different on Linux?
Aspose.Slides can only use fonts that are installed on the machine that renders the slides. When a presentation uses a font that is missing, such as Calibri on a typical Linux server, Aspose.Slides uses an installed font in its place, which can change the look of the text and where lines break. Install the fonts that your presentations use to get the same images as on Windows.
Why does getThumbnailWithImageSize fail with a TypeError?
The package README uses getThumbnailWithImageSize, but the package has no getThumbnail methods. Use getImageWithImageSize instead; it takes the same { width, height } argument.