使用Aspose.Slides对PPT进行操作

前言

开发.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://docs.aspose.com/slides/net/open-presentation/

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/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安替-AnTi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值