前言
開發(fā).NET項目常用的和office相關(guān)的庫有開源組件Apose.Slides和Spire.Presentation,以及微軟自帶的Microsoft.office.interop.PowerPoint組件。
微軟自帶的Microsoft.office.interop.PowerPoint組件雖然免費,但是需要在服務(wù)器端安裝PowerPoint,且需要配置DCOM組件權(quán)限,并且原生方法有限,導入附件、圖片等功能收到限制,經(jīng)過權(quán)衡,放棄使用微軟自帶的組件。
Apose.Slides和Spire.Presentation這兩個都是商業(yè)軟件,試用版和免費版的只有能生成前10頁,還帶有水印。Free Spire.Presentation可以沒有能超過十頁,否則需要購買商業(yè)付費版。
但是網(wǎng)上存在Apose.Slides的破解版本,可以完美解鎖大部分常用功能,最終采用Apose.Slides.net 17.9作為最終的開發(fā)組件,經(jīng)過測試滿足此次開發(fā)需求。
Aspose.Slides下載地址
https://download.csdn.net/download/weixin_35770067/86630578
開發(fā)環(huán)境
Visual Studio 2019
Microsoft Visual Studio是微軟公司的開發(fā)工具包系列產(chǎn)品。VS是一個基本完整的開發(fā)工具集,它包括了整個軟件生命周期中所需要的大部分工具,如UML工具、代碼管控工具、集成開發(fā)環(huán)境(IDE)等等。所寫的目標代碼適用于微軟支持的所有平臺。Visual Studio包含基于組件的開發(fā)工具(如Visual C#、Visual J#、Visual Basic和Visual C++),以及許多用于簡化基于小組的解決方案的設(shè)計、開發(fā)和部署的其他技術(shù)。
這里我們主要使用的是Visual Studio的Visual C#開發(fā)工具。
Microsoft開發(fā)組件
Apose.Slides.dll
開發(fā)過程中,需要引用上述類庫,用于創(chuàng)建、操作、保存PPT等操作。文章來源:http://www.zghlxwxcb.cn/news/detail-494093.html
官方文檔
https://docs.aspose.com/slides/net/open-presentation/文章來源地址http://www.zghlxwxcb.cn/news/detail-494093.html
Apose.Slides.dll 主要功能函數(shù)
創(chuàng)建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/
到了這里,關(guān)于使用Aspose.Slides對PPT進行操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!