前言
开发.NET项目常用的和office相关的库有开源组件Apose.Slides和Spire.Presentation,以及微软自带的Microsoft.office.interop.PowerPoint组件。
微软自带的Microsoft.office.interop.PowerPoint组件虽然免费,但是需要在服务器端安装PowerPoint,且需要配置DCOM组件权限,并且原生方法有限,导入附件、图片等功能收到限制,经过权衡,放弃使用微软自带的组件。
Apose.Slides和Spire.Presentation这两个都是商业软件,试用版和免费版的只有能生成前10页,还带有水印。Free Spire.Presentation可以没有能超过十页,否则需要购买商业付费版。
但是网上存在Apose.Slides的破解版本,可以完美解锁大部分常用功能,最终采用Apose.Slides.net 17.9作为最终的开发组件,经过测试满足此次开发需求。
Aspose.Slides下载地址
https://download.csdn.net/download/weixin_35770067/86630578
开发环境
Visual Studio 2019
Microsoft Visual Studio是微软公司的开发工具包系列产品。VS是一个基本完整的开发工具集,它包括了整个软件生命周期中所需要的大部分工具,如UML工具、代码管控工具、集成开发环境(IDE)等等。所写的目标代码适用于微软支持的所有平台。Visual Studio包含基于组件的开发工具(如Visual C#、Visual J#、Visual Basic和Visual C++),以及许多用于简化基于小组的解决方案的设计、开发和部署的其他技术。
这里我们主要使用的是Visual Studio的Visual C#开发工具。
Microsoft开发组件
Apose.Slides.dll
开发过程中,需要引用上述类库,用于创建、操作、保存PPT等操作。文章来源:https://www.toymoban.com/news/detail-494093.html
官方文档
https://docs.aspose.com/slides/net/open-presentation/文章来源地址https://www.toymoban.com/news/detail-494093.html
Apose.Slides.dll 主要功能函数
创建PPT
// Instantiate a Presentation object that represents a presentation fileusing (Presentation presentation = new Presentation()){
// Get the first slide ISlide slide = presentation.Slides[0];
// Add an autoshape of type line slide.Shapes.AddAutoShape(ShapeType.Line, 50, 150, 300, 0);
presentation.Save("NewPresentation_out.pptx", SaveFormat.Pptx);}
保存PPT
// Instantiate a Presentation object that represents a PPT file
Presentation presentation= new Presentation();
//...do some work here...// Save your presentation to a filepresentation.Save("Saved_out.pptx",
Aspose.Slides.Export.SaveFormat.Pptx);
增加PPT
// Instantiate Presentation class that represents a presentation file
using (Presentation pres = new Presentation("CloneWithinSamePresentationToEnd.pptx")){
// Clone the desired slide to the end of the collection of slides in the same presentation
ISlideCollection slds = pres.Slides;
slds.AddClone(pres.Slides[0]);
// Write the modified presentation to disk
pres.Save("Aspose_CloneWithinSamePresentationToEnd_out.pptx", SaveFormat.Pptx);
}
删除PPT
// Instantiate a Presentation object that represents a presentation file
using (Presentation pres = new Presentation("RemoveSlideUsingReference.pptx")){
// Accessing a slide using its index in the slides collection
ISlide slide = pres.Slides[0];
// Removing a slide using its reference
pres.Slides.Remove(slide);
//Writing the presentation file
pres.Save("modified_out.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}
修改PPT文本
using(Presentation pres = new Presentation("text.pptx")){
foreach (ISlide slide in pres.Slides)
{
foreach (IShape shape in slide.Shapes)
{
if (shape is IAutoShape autoShape) //Checks if shape supports text frame (IAutoShape). {
foreach (IParagraph paragraph in autoShape.TextFrame.Paragraphs) //Iterates through paragraphs in text frame {
foreach (IPortion portion in paragraph.Portions) //Iterates through each portion in paragraph {
portion.Text = portion.Text.Replace("years", "months"); //Changes text portion.PortionFormat.FontBold = NullableBool.True; //Changes formatting }
}
}
}
}
//Saves the modified presentation
pres.Save("text-changed.pptx", SaveFormat.Pptx);}
增加图片导入
using (Presentation pres = new Presentation()){
ISlide slide = pres.Slides[0];
IPPImage image = pres.Images.AddImage(File.ReadAllBytes("image.png"));
slide.Shapes.AddPictureFrame(ShapeType.Rectangle, 10, 10, 100, 100, image);
pres.Save("pres.pptx", SaveFormat.Pptx);}
增加视频导入
// Instantiate Presentation class that represents the PPTX
using (Presentation pres = new Presentation()){
// Get the first slide
ISlide sld = pres.Slides[0];
// Embedd vide inside presentation
IVideo vid = pres.Videos.AddVideo(new FileStream("Wildlife.mp4", FileMode.Open));
// Add Video Frame
IVideoFrame vf = sld.Shapes.AddVideoFrame(50, 150, 300, 350, vid);
// Set video to Video Frame
vf.EmbeddedVideo = vid;
// Set Play Mode and Volume of the Video
vf.PlayMode = VideoPlayModePreset.Auto;
vf.Volume = AudioVolumeMode.Loud;
// Write the PPTX file to disk
pres.Save("VideoFrame_out.pptx", SaveFormat.Pptx);}
增肌OLE文件导入
using (Presentation pres = new Presentation()){
ISlide slide = pres.Slides[0];
byte[] htmlBytes = File.ReadAllBytes("embedOle.html");
IOleEmbeddedDataInfo dataInfoHtml = new OleEmbeddedDataInfo(htmlBytes, "html");
IOleObjectFrame oleFrameHtml = slide.Shapes.AddOleObjectFrame(150, 120, 50, 50, dataInfoHtml);
oleFrameHtml.IsObjectIcon = true;
byte[] zipBytes = File.ReadAllBytes("embedOle.zip");
IOleEmbeddedDataInfo dataInfoZip = new OleEmbeddedDataInfo(zipBytes, "zip");
IOleObjectFrame oleFrameZip = slide.Shapes.AddOleObjectFrame(150, 220, 50, 50, dataInfoZip);
oleFrameZip.IsObjectIcon = true;
pres.Save("embeddedOle.pptx", SaveFormat.Pptx);}
增加文本框
// Instantiates PresentationExusing (Presentation pres = new Presentation()){
// Gets the first slide in the presentation
ISlide sld = pres.Slides[0];
// Adds an AutoShape with type set as Rectangle
IAutoShape ashp = sld.Shapes.AddAutoShape(ShapeType.Rectangle, 150, 75, 150, 50);
// Adds TextFrame to the Rectangle
ashp.AddTextFrame(" ");
// Accesses the text frame
ITextFrame txtFrame = ashp.TextFrame;
// Creates the Paragraph object for text frame
IParagraph para = txtFrame.Paragraphs[0];
// Creates a Portion object for the paragraph
IPortion portion = para.Portions[0];
// Sets the text
portion.Text = "Aspose TextBox";
// Saves the presentation to disk
pres.Save("TextBox_out.pptx", Aspose.Slides.Export.SaveFormat.Pptx);}
参考文献
- https://docs.aspose.com/slides/net/create-presentation/
到了这里,关于使用Aspose.Slides对PPT进行操作的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!