Create PowerPoint in Node.js

The Microsoft PowerPoint app (on Windows, macOS, and other platforms) allows you to create presentations and slide shows containing texts, images, charts, animations, and many other elements.

By the time you finish reading this article, you’ll have learned how to create PowerPoint in Node.js and add all kinds of content to a presentation by running code in node.js.

Get Node.js PowerPoint API

Aspose.Slides Node.js PowerPoint API

Aspose.Slides for Node.js via Java is a powerful API that provides everything developers and applications need to create, open, convert, and manipulate PowerPoint documents in node.js and javascript server-side applications.

You can install Aspose.Slides for Node.js via Java from NPM by running this command:

npm install aspose.slides.via.java

If you encounter any problems during the installation process, see this product page.

Notes:

Create PowerPoint PPT in Node.js

  1. Create an instance of the Presentation class.
  2. Save the object through the Presentation.save(String, SaveFormat) method.
// Instantiate a Presentation object that represents a presentation file
var presentation = new aspose.slides.Presentation();

// Get the first slide
var slide = presentation.getSlides().get_Item(0);

// Add content to slide...

// Save presentation
presentation.save("NewPresentation.pptx", aspose.slides.SaveFormat.Pptx);

Open PowerPoint PPT in Node.js

  1. Create an instance of the Presentation class and pass the path to the PowerPoint you want to open to the class construction.
  2. Perform a task. You can add some content to a slide. You can do nothing.
  3. Save the presentation.
// Instantiate a Presentation object that represents a presentation file
var presentation = new aspose.slides.Presentation("presentation.pptx");

// Get the first slide
var slide = presentation.getSlides().get_Item(0);

// Add content to slide...

// Save presentation
presentation.save("NewPresentation.pptx", aspose.slides.SaveFormat.Pptx);

Add Slide to PowerPoint PPTX in Node.js

  1. Create an instance of the Presentation class and pass the path to the PowerPoint where you want to add a slide.
  2. Instantiate the ISlideCollection class by setting a reference to the getSlides() method.
  3. Add an empty slide to the presentation through the addEmptySlide(ILayoutSlide) method exposed by ISlideCollection object.
  4. Save the updated presentation using the Presentation.save(String, SaveFormat) method.
// Instantiate a Presentation object that represents a presentation file
var presentation = new aspose.slides.Presentation("presentation.pptx");

// Access the slides collection
var slds = presentation.getSlides();

for (var i = 0; i < presentation.getLayoutSlides().size(); i++) {
	// Add an empty slide to the Slides collection
	slds.addEmptySlide(presentation.getLayoutSlides().get_Item(i));
}

// Save presentation
presentation.save("NewPresentation.pptx", aspose.slides.SaveFormat.Pptx);

Add Text to PowerPoint PPT

  1. Create an instance of the Presentation class and pass the path to the PowerPoint where you want to add some text.
  2. Get the reference of the slide where you want to add some text through its index.
  3. Add a rectangle through the addAutoShape() method and get its reference in IAutoShape object.
  4. Add a TextFrame to the shape containing text.
  5. Set your preferred properties for the text, such as fill color, fill type, etc.
  6. Save the updated presentation through the save(String, SaveFormat) method.
var colorBlack = java.getStaticFieldValue("java.awt.Color", "BLACK");
var colorWhite = java.getStaticFieldValue("java.awt.Color", "WHITE");

// Instantiate a Presentation object that represents a presentation file
var presentation = new aspose.slides.Presentation("presentation.pptx");

// Get the first slide
var sld = presentation.getSlides().get_Item(0);

// Add an AutoShape of Rectangle type
var ashp = sld.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 150, 75, 150, 50);

// Add ITextFrame to the Rectangle
ashp.addTextFrame("Hello World");

// Change the text color to Black (which is White by default)
ashp.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat().getFillFormat()
		.setFillType(java.newByte(aspose.slides.FillType.Solid));
ashp.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat().getFillFormat()
		.getSolidFillColor().setColor(colorBlack);

// Change the line color of the rectangle to White
ashp.getShapeStyle().getLineColor().setColor(colorWhite);

// Remove any fill formatting in the shape
ashp.getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));

// Save presentation
presentation.save("NewPresentation.pptx", aspose.slides.SaveFormat.Pptx);

Create Chart in PowerPoint PPT

  1. Create an instance of the Presentation class and pass the path to the PowerPoint where you want to create a chart.
  2. Get the reference of the slide where you want to create a chart through its index.
  3. Add your preferred chart through the addChart method.
  4. Add a chart title.
  5. Access the chart data worksheet.
  6. Clear all the default series and categories.
  7. Add new series and categories.
  8. Add new chart data for the chart series.
  9. Set a fill color for the chart series.
  10. Add chart series labels.
  11. Save the presentation as a PPT file.
var colorGreen = java.getStaticFieldValue("java.awt.Color", "GREEN");
var colorRed = java.getStaticFieldValue("java.awt.Color", "RED");

// Instantiate a presentation class that represents a PPTX file
var pres = new aspose.slides.Presentation();

// Access the first slide
var sld = pres.getSlides().get_Item(0);

// Add a chart with its default data
var chart = sld.getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 0, 0, 500, 500);

// Set the chart Title
chart.getChartTitle().addTextFrameForOverriding("Sample Title");
chart.getChartTitle().getTextFrameForOverriding().getTextFrameFormat().setCenterText(java.newByte(aspose.slides.NullableBool.True));
chart.getChartTitle().setHeight(20);
chart.hasTitle();

// Set the first series to show values
chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowValue(true);

// Set the index for the chart data sheet
var defaultWorksheetIndex = 0;

// Get the chart data WorkSheet
var fact = chart.getChartData().getChartDataWorkbook();

// Delete the default generated series and categories
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
var s = chart.getChartData().getSeries().size();
s = chart.getChartData().getCategories().size();

// Add new series
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 1, "Series 1"),chart.getType());
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 2, "Series 2"),chart.getType());

// Add new categories
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 1, 0, "Caetegoty 1"));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 2, 0, "Caetegoty 2"));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 3, 0, "Caetegoty 3"));

// Take the first chart series
var series = chart.getChartData().getSeries().get_Item(0);

// Now populates the series data
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 1, 1, 20));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 50));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 3, 1, 30));

// Set the fill color for series
series.getFormat().getFill().setFillType(java.newByte(aspose.slides.FillType.Solid));
series.getFormat().getFill().getSolidFillColor().setColor(colorRed);

// Take the second chart series
series = chart.getChartData().getSeries().get_Item(1);

// Populate series data
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 1, 2, 30));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 2, 2, 10));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 3, 2, 60));

// Set the fill color for the series
series.getFormat().getFill().setFillType(java.newByte(aspose.slides.FillType.Solid));
series.getFormat().getFill().getSolidFillColor().setColor(colorGreen);

//Create custom labels for each categories for the new series
// Set the first label to show Category name
var lbl = series.getDataPoints().get_Item(0).getLabel();
lbl.getDataLabelFormat().setShowCategoryName(true);

// Set the second label to show Series name
lbl = series.getDataPoints().get_Item(1).getLabel();
lbl.getDataLabelFormat().setShowSeriesName(true);

// Show value for the third label
lbl = series.getDataPoints().get_Item(2).getLabel();
lbl.getDataLabelFormat().setShowValue(true);
lbl.getDataLabelFormat().setShowSeriesName(true);
lbl.getDataLabelFormat().setSeparator("/");

// Save the presentation with chart
pres.save("NewPresentation.pptx", aspose.slides.SaveFormat.Pptx);

Add Image to PPT in Node.js

  1. Create an instance of the Presentation class.
  2. Get the reference of the slide where you want to add an image through its index.
  3. Create an IPPImage object by adding an image to the IImagescollection associated with the presentation object that will be used to fill the shape.
  4. Specify the image’s width and height.
  5. Create a PictureFrame based on the image’s width and height through the AddPictureFrame method exposed by the shape object associated with the referenced slide.
  6. Add a picture frame (containing the picture) to the slide.
  7. Save the presentation as a PPT file.
var fileStream = fs.createReadStream("image.png");
aspose.slides.readBytesFromStream(fileStream, function (imgArray) {
    var pres = new aspose.slides.Presentation();
    var img = pres.getImages().addImage(imgArray);
    pres.getSlides().get_Item(0).getShapes().addPictureFrame(aspose.slides.ShapeType.Rectangle, 10, 10, 200, 200, img); 
    pres.save("NewPresentation.pptx", aspose.slides.SaveFormat.Pptx);    
});

Conclusion

In this article, we walked you through operations on creating PowerPoint in Node.js and performing other tasks by simply running a few lines of Node.js code. You may be interested in learning more about the powerful PowerPoint API that makes everything easy.

See Also