C#向PPT文档插入图片以及导出图片
PowerPoint演示文稿是我们日常工作中常用的办公软件之一,而图片则是PowerPoint文档的重要组成部分,那么如何向幻灯片插入图片以及导出图片呢?本文我将给大家分享如何使用一个免费版PowerPoint组件—Free Spire.Presentation,以C#/VB.NET编程的方式来快速地实现这两个功能。我们可以从官网下载Free Spire.Presentation,创建项目后添加此DLL作为引用。
插入图片
向PPT文档插入图片时,这里我选择插入两张图片到不同的两张幻灯片中。
具体步骤:
在之前需要添加以下命名空间:
using Spire.Presentation;
using Spire.Presentation.Drawing;
步骤1:新建一个PPT文档。
Presentation presentation = new Presentation(); presentation.Slides.Append();
步骤2:插入第一张图片到第一张幻灯片
string ImageFile = @"C:\Users\Administrator\Pictures\01.jpg";
RectangleF rect = new RectangleF(, , , );
presentation.Slides[].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect);
presentation.Slides[].Shapes[].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite;
步骤3:添加形状,再添加文本到形状里面。
RectangleF rect2 = new RectangleF(, , , );
IAutoShape shape = presentation.Slides[].Shapes.AppendShape(ShapeType.Rectangle, rect2);
shape.Fill.FillType = FillFormatType.None;
shape.ShapeStyle.LineColor.Color = Color.White; //添加文本到形状中
shape.TextFrame.Text = "大熊猫是哺乳动物,已在地球上生存了至少800万年,被誉为活化石和中国国宝,世界自然基金会的形象大使,是世界生物多样性保护的旗舰物种。据第三次全国大熊猫野外种群调查,全世界野生大熊猫已不足1600只,属于中国国家一级保护动物。";
TextRange textRange = shape.TextFrame.TextRange;
shape.TextFrame.Paragraphs[].Alignment = TextAlignmentType.Left; //设置文本字体
textRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
textRange.Fill.SolidColor.Color = Color.Black;
textRange.LatinFont = new TextFont("Arial Black"
步骤4:同样,插入第二张图片到第二张幻灯片,添加形状,再添加文本到形状里面。最后保存文档。
presentation.SaveToFile(@"C:\Users\Administrator\Desktop\result.pptx ", FileFormat.Pptx2010);
System.Diagnostics.Process.Start(@"C:\Users\Administrator\Desktop\result.pptx ");
效果图:

全部代码:
using System;
using System.Drawing;
using System.Windows.Forms;
using Spire.Presentation;
using Spire.Presentation.Drawing; namespace InsertimageinPowerPointFille
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
//新建PPT
Presentation presentation = new Presentation();
presentation.Slides.Append(); //插入第一张图片到第一张幻灯片
string ImageFile = @"C:\Users\Administrator\Pictures\01.jpg";
RectangleF rect = new RectangleF(, , , );
presentation.Slides[].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect);
presentation.Slides[].Shapes[].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite; //添加形状
RectangleF rect2 = new RectangleF(, , , );
IAutoShape shape = presentation.Slides[].Shapes.AppendShape(ShapeType.Rectangle, rect2);
shape.Fill.FillType = FillFormatType.None;
shape.ShapeStyle.LineColor.Color = Color.White; //添加文本到形状中
shape.TextFrame.Text = "大熊猫是哺乳动物,已在地球上生存了至少800万年,被誉为活化石和中国国宝,世界自然基金会的形象大使,是世界生物多样性保护的旗舰物种。据第三次全国大熊猫野外种群调查,全世界野生大熊猫已不足1600只,属于中国国家一级保护动物。";
TextRange textRange = shape.TextFrame.TextRange;
shape.TextFrame.Paragraphs[].Alignment = TextAlignmentType.Left; //设置文本字体
textRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
textRange.Fill.SolidColor.Color = Color.Black;
textRange.LatinFont = new TextFont("Arial Black"); //插入第二张图片到第二张幻灯片
string ImageFile1 = @"C:\Users\Administrator\Pictures\02.jpg";
RectangleF rect1 = new RectangleF(, , , );
presentation.Slides[].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile1, rect1);
presentation.Slides[].Shapes[].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite; //添加形状
RectangleF rect3 = new RectangleF(, , , );
IAutoShape shape1 = presentation.Slides[].Shapes.AppendShape(ShapeType.Rectangle, rect3);
shape1.Fill.FillType = FillFormatType.Solid;
shape1.Fill.FillType = FillFormatType.None;
shape1.ShapeStyle.LineColor.Color = Color.White; //添加文本到形状中
shape1.TextFrame.Text = "黑白相间的外表,有利隐蔽在密林的树上和积雪的地面而不易被天敌发现。相对锋利的爪和发达有力的前后肢,有利于大熊猫能快速爬上高大的乔木。";
TextRange textRange1 = shape1.TextFrame.TextRange; //设置文本字体
textRange1.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
textRange1.Fill.SolidColor.Color = Color.Blue;
textRange1.LatinFont = new TextFont("Arial Black"); //保存文件
presentation.SaveToFile(@"C:\Users\Administrator\Desktop\result.pptx ", FileFormat.Pptx2010);
System.Diagnostics.Process.Start(@"C:\Users\Administrator\Desktop\result.pptx ");
}
}
}
从上面的代码可以发现,其实通过这个组件,我们还可以自由地设置我们想要的形状、文本、字体、颜色等等,用起来确实方便又快速。感兴趣的话可以试一下其他丰富的效果。
导出图片
现在,我们导出上述运行后文档的图片。
具体步骤:
同样添加如下命名空间:
using Spire.Presentation;
步骤1: 新建一个Presentation对象,并加载Presentation文件。
Presentation ppt = new Presentation();
ppt.LoadFromFile(@"C:\Users\Administrator\Desktop\result.pptx");
步骤2:遍历PPT文档所有的图片,并保存为.png格式。
for (int i = ; i < ppt.Images.Count; i++)
{
Image image = ppt.Images[i].Image;
image.Save(string.Format(@"..\..\Images{0}.png", i));
}
效果图:

全部代码:
using System;
using System.Drawing;
using System.Windows.Forms;
using Spire.Presentation; namespace ExtractImagesfromPPT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
Presentation ppt = new Presentation();
ppt.LoadFromFile(@"C:\Users\Administrator\Desktop\result.pptx");
for (int i = ; i < ppt.Images.Count; i++)
{
Image image = ppt.Images[i].Image;
image.Save(string.Format(@"..\..\Images{0}.png", i)); }
}
}
}
谢谢浏览!
C#向PPT文档插入图片以及导出图片的更多相关文章
- C#/VB.NET 向PowerPoint文档插入视频
如今,Microsoft Office PowerPoint在我们日常生活中的应用已经很广泛了,利用Microsoft Office PowerPoint不仅可以创建演示文稿,还可以在互联网上召开面对 ...
- C# 复制幻灯片(包括格式、背景、图片等)到同/另一个PPT文档
C# 复制幻灯片(包括格式.背景.图片等)到同/另一个PPT文档 复制幻灯片是使用PowerPoint过程中的一个比较常见的操作,在复制一张幻灯片时一般有以下两种情况: 在同一个PPT文档内复制 从一 ...
- 使用POI操作PPT文档(插入文本、图片)转
1)如果是创建新的PPT文档,直接使用SlideShow和Slide类就可以,其中SlideShow表示PPT文档,Slide表示某一张幻灯片如下代码创建空的PPT文档: SlideShow ppt ...
- 在网页中在线浏览ppt文档
方法一: 把ppt文件的扩展名直接修改为pps,嵌入到网页中 缺点:这种方式浏览器会提示是打开,还是下载,选择打开的话会直接在浏览器中打开,并且客户端一定要安装Office PowerPoint才能打 ...
- 如何使用免费PDF控件从PDF文档中提取文本和图片
如何使用免费PDF控件从PDF文档中提取文本和图片 概要 现在手头的项目有一个需求是从PDF文档中提取文本和图片,我以前也使用过像iTextSharp, PDFBox 这些免费的PD ...
- C#如何向word文档插入一个新段落及隐藏段落
编辑Word文档时,我们有时会突然想增加一段新内容:而将word文档给他人浏览时,有些信息我们是不想让他人看到的.那么如何运用C#编程的方式巧妙地插入或隐藏段落呢?本文将与大家分享一种向Word文档插 ...
- C# 实现对PPT文档加密、解密以及重置密码的操作
工作中我们会使用到各种各样的文档,其中,PPT起着不可或缺的作用.一份PPT文档里可能包含重要商业计划.企业运营资料或者公司管理资料等.因此,在竞争环境里,企业重要资料的保密工作就显得尤为重要,而对于 ...
- Atitit 计算word ppt文档的页数
Atitit 计算word ppt文档的页数 http://localhost:8888/ http://git.oschina.net/attilax/ati_wordutil private vo ...
- 在线HTML文档编辑器使用入门之图片上传与图片管理的实现
在线HTML文档编辑器使用入门之图片上传与图片管理的实现: 官方网址: http://kindeditor.net/demo.php 开发步骤: 1.开发中只需要导入选中的文件(通常在 webapp ...
随机推荐
- RecyclerView使用大全
RecylerView介绍 RecylerView是support-v7包中的新组件,是一个强大的滑动组件,与经典的ListView相比,同样拥有item回收复用的功能,这一点从它的名字recyler ...
- StringMVC 中如何做数据校验
步骤一:引入四个jar包 步骤二:注册类型转换器 <context:component-scan base-package="cn.happy.controller"> ...
- 游戏编程系列[1]--游戏编程中RPC协议的使用[3]--体验
运行环境,客户端一般编译为.Net 3.5 Unity兼容,服务端因为用了一些库,所以一般为4.0 或往上.同一份代码,建立拥有2个项目.客户端引用: WindNet.Client服务端引用: OpL ...
- Asp.net MVC 传递数据 从前台到后台,包括单个对象,多个对象,集合
今天为大家分享下 Asp.net MVC 将数据从前台传递到后台的几种方式. 环境:VS2013,MVC5.0框架 1.基本数据类型 我们常见有传递 int, string, bool, double ...
- [C#] 回眸 C# 的前世今生 - 见证 C# 6.0 的新语法特性
回眸 C# 的前世今生 - 见证 C# 6.0 的新语法特性 序 目前最新的版本是 C# 7.0,VS 的最新版本为 Visual Studio 2017 RC,两者都尚未进入正式阶段.C# 6.0 ...
- 设计模式之结构类模式大PK
结构类模式大PK 结构类模式包括适配器模式.桥梁模式.组合模式.装饰模式.门面模式.享元模式和代理模式.之所以称其为结构类模式,是因 ...
- javascript动画系列第一篇——模拟拖拽
× 目录 [1]原理介绍 [2]代码实现 [3]代码优化[4]拖拽冲突[5]IE兼容 前面的话 从本文开始,介绍javascript动画系列.javascript本身是具有原生拖放功能的,但是由于兼容 ...
- Jvm 内存浅析 及 GC个人学习总结
从诞生至今,20多年过去,Java至今仍是使用最为广泛的语言.这仰赖于Java提供的各种技术和特性,让开发人员能优雅的编写高效的程序.今天我们就来说说Java的一项基本但非常重要的技术内存管理 了解C ...
- 基于Composer Player 模型加载和相关属性设置
主要是基于达索软件Composer Player.的基础上做些二次开发. public class ComposerToolBarSetting { public bool AntiAliasingO ...
- 新手学习web遇到的一些乱码问题
在新手学习web网站学习的时候经常会遇到?????这种乱码,对于刚起步的菜鸟来说真的很头痛,很容易打击继续学的信心当然了对于菜鸟的我最近也遇到过乱码问题,沉浸其中不能自拔,爱的深啊!!!!!我所遇到的 ...