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文档插入图片以及导出图片的更多相关文章

  1. C#/VB.NET 向PowerPoint文档插入视频

    如今,Microsoft Office PowerPoint在我们日常生活中的应用已经很广泛了,利用Microsoft Office PowerPoint不仅可以创建演示文稿,还可以在互联网上召开面对 ...

  2. C# 复制幻灯片(包括格式、背景、图片等)到同/另一个PPT文档

    C# 复制幻灯片(包括格式.背景.图片等)到同/另一个PPT文档 复制幻灯片是使用PowerPoint过程中的一个比较常见的操作,在复制一张幻灯片时一般有以下两种情况: 在同一个PPT文档内复制 从一 ...

  3. 使用POI操作PPT文档(插入文本、图片)转

    1)如果是创建新的PPT文档,直接使用SlideShow和Slide类就可以,其中SlideShow表示PPT文档,Slide表示某一张幻灯片如下代码创建空的PPT文档: SlideShow ppt ...

  4. 在网页中在线浏览ppt文档

    方法一: 把ppt文件的扩展名直接修改为pps,嵌入到网页中 缺点:这种方式浏览器会提示是打开,还是下载,选择打开的话会直接在浏览器中打开,并且客户端一定要安装Office PowerPoint才能打 ...

  5. 如何使用免费PDF控件从PDF文档中提取文本和图片

             如何使用免费PDF控件从PDF文档中提取文本和图片 概要 现在手头的项目有一个需求是从PDF文档中提取文本和图片,我以前也使用过像iTextSharp, PDFBox 这些免费的PD ...

  6. C#如何向word文档插入一个新段落及隐藏段落

    编辑Word文档时,我们有时会突然想增加一段新内容:而将word文档给他人浏览时,有些信息我们是不想让他人看到的.那么如何运用C#编程的方式巧妙地插入或隐藏段落呢?本文将与大家分享一种向Word文档插 ...

  7. C# 实现对PPT文档加密、解密以及重置密码的操作

    工作中我们会使用到各种各样的文档,其中,PPT起着不可或缺的作用.一份PPT文档里可能包含重要商业计划.企业运营资料或者公司管理资料等.因此,在竞争环境里,企业重要资料的保密工作就显得尤为重要,而对于 ...

  8. Atitit 计算word ppt文档的页数

    Atitit 计算word ppt文档的页数 http://localhost:8888/ http://git.oschina.net/attilax/ati_wordutil private vo ...

  9. 在线HTML文档编辑器使用入门之图片上传与图片管理的实现

    在线HTML文档编辑器使用入门之图片上传与图片管理的实现: 官方网址: http://kindeditor.net/demo.php 开发步骤: 1.开发中只需要导入选中的文件(通常在 webapp ...

随机推荐

  1. Python 小而美的函数

    python提供了一些有趣且实用的函数,如any all zip,这些函数能够大幅简化我们得代码,可以更优雅的处理可迭代的对象,同时使用的时候也得注意一些情况   any any(iterable) ...

  2. bootstrap-datetimepicker 进一步跟进~~~开始时间和结束时间的样式显示

    上次简单介绍了一下:05.LoT.UI 前后台通用框架分解系列之——漂亮的时间选择器(http://www.cnblogs.com/dunitian/p/5524019.html) 这次深入再介绍一下 ...

  3. nodejs进阶(1)—输出hello world

    下面将带领大家一步步学习nodejs,知道怎么使用nodejs搭建服务器,响应get/post请求,连接数据库等. 搭建服务器页面输出hello world var  http  =  require ...

  4. 立即执行函数表达式(IIFE)

    原文地址:benalman.com/news/2010/11/immediately-invoked-function-expression/ 译者:nzbin 也许你还没有注意到,我是一个对术语比较 ...

  5. 微信小程序中利用时间选择器和js无计算实现定时器(将字符串或秒数转换成倒计时)

    转载注明出处 改成了一个单独的js文件,并修改代码增加了通用性,点击这里查看 今天写小程序,有一个需求就是用户选择时间,然后我这边就要开始倒计时. 因为小程序的限制,所以直接选用时间选择器作为选择定时 ...

  6. 【手把手】JavaWeb 入门级项目实战 -- 文章发布系统 (第十二节)

    好的,那么在上一节中呢,评论功能的后台已经写好了,这一节,先把这部分后台代码和前台对接一下. 1.评论功能实现 我们修改一下保存评论按钮的点击事件,用jQuery的方式获取文本框中的值,然后通过aja ...

  7. 一行代码实现java list去重

    1.不带类型写法: 1 List listWithoutDup = new ArrayList(new HashSet(listWithDup)); 2.带类型写法(以String类型为例):1)Ja ...

  8. 如何使用swing创建一个BeatBox

    首先,我们需要回顾一些内容(2017-01-04 14:32:14): 1.Swing组件 Swing的组件(component,或者称之为元件),是较widget更为正确的术语,它们就是会放在GUI ...

  9. 反应器(Reactor)和主动器(Proactor)

    网络方面用的比较多的库是libevent和boost.asio,两者都是跨平台的.其中libevent是基于Reactor实现的,而boost.asio是基于Proactor实现的.Reactor和P ...

  10. 报错:You need to use a Theme.AppCompat theme (or descendant) with this activity.

    学习 Activity 生命周期时希望通过 Dialog 主题测试 onPause() 和 onStop() 的区别,点击按钮跳转 Activity 时报错: E/AndroidRuntime: FA ...