个人中心

联系我们

搜索
搜索 登录 免费注册
界面美化
业务优化
开发工具
图像管理
文档管理
Parasoft

新闻资讯

关注工具软件产品最新动态,了解软件开发行业新趋势。

Aspose 使用教程:在 Node.js 中创建和打开 PPT

原创
软件开发
来源:Aspose
Aspose
PPT
JavaScript
.slides
2023-12-26
Aspose
PPT
JavaScript
.slides


Microsoft PowerPoint 应用程序(Windows、macOS 和其他平台)允许您创建包含文本、图像、图表、动画和许多其他元素的演示文稿和幻灯片。


当您读完本文时,您将学会如何在 Node.js 中创建 PowerPoint,并通过在 node.js 中运行代码为演示文稿添加各种内容。


获取 Node.js PowerPoint API

在 Node.js 中创建 PowerPoint

在 Node.js 中打开 PowerPoint

向 PowerPoint 添加幻灯片

向 PowerPoint 添加文本

在 PowerPoint 中创建图表

在 PowerPoint 中添加图片


获取 Node.js PowerPoint API



Aspose.Slides for Node.js via Java 是一个功能强大的应用程序接口,可为开发人员和应用程序提供在 node.js 和 javascript 服务器端应用程序中创建、打开、转换和操作 PowerPoint 文档所需的一切。


您可以通过运行以下命令从 NPM 安装 Aspose.Slides for Node.js via Java:


npm install aspose.slides.via.java


如果在安装过程中遇到任何问题,请参阅本产品页面。


注意事项


有关其他安装方法,请参阅我们文档中的安装文章。

请注意 Node.js 和 Java 之间的产品限制和差异。请参阅我们文档中的限制和差异文章以及 nodejs-jave 桥接产品页面。

在 Node.js 中创建 PowerPoint PPT

创建 Presentation 类的实例。

通过 Presentation.save(String, SaveFormat) 方法保存对象。


// 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);


在 Node.js 中打开 PowerPoint PPT

创建 Presentation 类的实例,并将要打开的 PowerPoint 的路径传递给类的构造。

执行任务。您可以向幻灯片添加一些内容。也可以什么都不做。

保存演示文稿。


// 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);


在 Node.js 中向 PowerPoint PPTX 添加幻灯片

创建 Presentation 类的实例,并将路径传给要添加幻灯片的 PowerPoint。

通过设置对 getSlides() 方法的引用,实例化 ISlideCollection 类。

通过 ISlideCollection 对象暴露的 addEmptySlide(ILayoutSlide) 方法向演示文稿中添加一张空幻灯片。

使用 Presentation.save(String, SaveFormat) 方法保存更新后的演示文稿。


// 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);


在 PowerPoint PPT 中添加文本

创建一个 Presentation 类实例,并将路径传给要添加文本的 PowerPoint。

通过索引获取要添加文本的幻灯片的引用。

通过 addAutoShape() 方法添加一个矩形,并在 IAutoShape 对象中获取其引用。

为包含文本的形状添加一个 TextFrame。

设置文本的首选属性,如填充颜色、填充类型等。

通过 save(String, SaveFormat) 方法保存更新后的演示文稿。


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);


在 PowerPoint PPT 中创建图表

创建 Presentation 类的实例,并将路径传给要创建图表的 PowerPoint。

通过索引获取要创建图表的幻灯片的引用。

通过 addChart 方法添加您喜欢的图表。

添加图表标题

访问图表数据工作表

清除所有默认系列和类别。

添加新的系列和类别

为图表系列添加新的图表数据

为图表系列设置填充颜色

添加图表系列标签

将演示文稿保存为 PPT 文件。


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);


在 Node.js 中为 PPT 添加图片

创建 Presentation 类的实例。

通过索引获取要添加图片的幻灯片的引用。

通过向与演示对象关联的 IImagescollection 中添加图像来创建 IPPImage 对象,该图像将用于填充形状。

指定图片的宽度和高度。

通过与引用幻灯片关联的形状对象所公开的 AddPictureFrame 方法,根据图片的宽度和高度创建 PictureFrame。

将包含图片的相框添加到幻灯片中。

将演示文稿保存为 PPT 文件。


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);    

});


结论

在本文中,我们向您介绍了在 Node.js 中创建 PowerPoint 的操作,以及只需运行几行 Node.js 代码即可执行的其他任务。您可能有兴趣进一步了解让一切变得简单的强大 PowerPoint API。


联系我们

周一至周日 8:00-23:00

免费热线

023-62585653

张经理:13082556879

罗经理:17558866126

许经理:13057566525

开发外包

ERP-一体化

小程序

企业微信客服

版权所有:重庆庚乾信息科技有限公司 ©2025 Gengqian Information Technology Co., Ltd. 渝ICP备2022008063号-2 渝公网安备50010702505508

版权所有:重庆庚乾信息科技有限公司

©2025 Gengqian Information Technology Co., Ltd. 渝ICP备2022008063号-2 渝公网安备50010702505508