本文通过C#及VB.NET后端程序代码展示如何在PPT幻灯片中嵌入一个Zip文件。本次测试环境包括:
- .NET FrameWork 4.5.1
- Spire.Presentation.dll Version 6.1
关于引用dll
获取dll文件包,解压,将Bin文件夹下的dll文件添加引用至VS程序;或通过Nuget下载。
【C#】
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System;
using System.Drawing;
using System.IO;
namespace InsertZip
{
class Program
{
static void Main(string[] args)
{
//创建一个PPT文档,并获取第一张幻灯片(创建的PPT默认包含一张空白幻灯片)
Presentation ppt = new Presentation();
ISlide slide = ppt.Slides[0];
//读取zip文件到byte数组
byte[] data = File.ReadAllBytes("sample.zip");
//在幻灯片中绘制形状,并添加zip文件对象
Rectangle rec = new Rectangle(120, 120, 100, 100);
IOleObject ole = slide.Shapes.AppendOleObject("sample.zip", data, rec);
ole.ProgId = "Package";
//设置对象封面图片
Image image = Image.FromFile("logo.png");
IImageData oleImage = ppt.Images.Append(image);
ole.SubstituteImagePictureFillFormat.Picture.EmbedImage = oleImage;
//保存文档
ppt.SaveToFile("InsertZip.pptx", Spire.Presentation.FileFormat.Pptx2013);
System.Diagnostics.Process.Start("InsertZip.pptx");
}
}
}
嵌入效果:
注:本次测试中,所有测试文件路径均放在项目文件夹下,即C:\Users\Administrator\Documents\visual studio 2013\Projects\InsertOLE_PPT\InsertZip\bin\Debug,路径也可自行定义。
【vb.net】
Imports Spire.Presentation
Imports Spire.Presentation.Drawing
Imports System.Drawing
Imports System.IO
Namespace InsertZip
Class Program
Private Shared Sub Main(args As String())
'创建一个PPT文档,并获取第一张幻灯片(创建的PPT默认包含一张空白幻灯片)
Dim ppt As New Presentation()
Dim slide As ISlide = ppt.Slides(0)
'读取zip文件到byte数组
Dim data As Byte() = File.ReadAllBytes("sample.zip")
'在幻灯片中绘制形状,并添加zip文件对象
Dim rec As New Rectangle(120, 120, 100, 100)
Dim ole As IOleObject = slide.Shapes.AppendOleObject("sample.zip", data, rec)
ole.ProgId = "Package"
'设置对象封面图片
Dim image__1 As Image = Image.FromFile("logo.png")
Dim oleImage As IImageData = ppt.Images.Append(image__1)
ole.SubstituteImagePictureFillFormat.Picture.EmbedImage = oleImage
'保存文档
ppt.SaveToFile("InsertZip.pptx", Spire.Presentation.FileFormat.Pptx2013)
System.Diagnostics.Process.Start("InsertZip.pptx")
End Sub
End Class
End Namespace
来源:oschina
链接:https://my.oschina.net/u/4459574/blog/4900968