服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#向PPT文档插入图片以及导出图片的实例

C#向PPT文档插入图片以及导出图片的实例

2021-12-15 13:27Yesi C#

PowerPoint演示文稿是我们日常工作中常用的办公软件之一,本篇文章介绍了C#向PPT文档插入图片以及导出图片的实例,非常具有实用价值,需要的朋友可以参考下。

 powerpoint演示文稿是我们日常工作中常用的办公软件之一,而图片则是powerpoint文档的重要组成部分,那么如何向幻灯片插入图片以及导出图片呢?本文我将给大家分享如何使用一个免费版powerpoint组件—free spire.presentation,以c#/vb.net编程的方式来快速地实现这两个功能。我们可以从官网下载free spire.presentation,创建项目后添加此dll作为引用。

插入图片

向ppt文档插入图片时,这里我选择插入两张图片到不同的两张幻灯片中。

具体步骤:

在之前需要添加以下命名空间:

?
1
2
using spire.presentation;
using spire.presentation.drawing;

步骤1:新建一个ppt文档。

?
1
2
3
presentation presentation = new presentation();
 
presentation.slides.append();

步骤2:插入第一张图片到第一张幻灯片

?
1
2
3
4
string imagefile = @"c:\users\administrator\pictures\01.jpg";
rectanglef rect = new rectanglef(350, 100, 300, 250); 
presentation.slides[0].shapes.appendembedimage(shapetype.rectangle, imagefile, rect);
presentation.slides[0].shapes[0].line.fillformat.solidfillcolor.color = color.floralwhite;

步骤3:添加形状,再添加文本到形状里面。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
rectanglef rect2 = new rectanglef(50, 100, 300, 250);
iautoshape shape = presentation.slides[0].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[0].alignment = textalignmenttype.left;
 
 
//设置文本字体
textrange.fill.filltype = spire.presentation.drawing.fillformattype.solid;
textrange.fill.solidcolor.color = color.black;
textrange.latinfont = new textfont("arial black"

步骤4:同样,插入第二张图片到第二张幻灯片,添加形状,再添加文本到形状里面。最后保存文档。

?
1
2
presentation.savetofile(@"c:\users\administrator\desktop\result.pptx ", fileformat.pptx2010);
system.diagnostics.process.start(@"c:\users\administrator\desktop\result.pptx ");

效果图:

 C#向PPT文档插入图片以及导出图片的实例

全部代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using system;
using system.drawing;
using system.windows.forms;
using spire.presentation;
using spire.presentation.drawing;
 
namespace insertimageinpowerpointfille
{
 public partial class form : form
 {
  public form()
  {
   initializecomponent();
  }
 
  private void button_click(object sender, eventargs e)
  {
   //新建ppt
   presentation presentation = new presentation();
   presentation.slides.append();
 
   //插入第一张图片到第一张幻灯片
   string imagefile = @"c:\users\administrator\pictures\.jpg";
   rectanglef rect = new rectanglef(, , , );
   presentation.slides[].shapes.appendembedimage(shapetype.rectangle, imagefile, rect);
   presentation.slides[].shapes[].line.fillformat.solidfillcolor.color = color.floralwhite;
 
   //添加形状
   rectanglef rect = new rectanglef(, , , );
   iautoshape shape = presentation.slides[].shapes.appendshape(shapetype.rectangle, rect);
   shape.fill.filltype = fillformattype.none;
   shape.shapestyle.linecolor.color = color.white;
 
   //添加文本到形状中
   shape.textframe.text = "大熊猫是哺乳动物,已在地球上生存了至少万年,被誉为活化石和中国国宝,世界自然基金会的形象大使,是世界生物多样性保护的旗舰物种。据第三次全国大熊猫野外种群调查,全世界野生大熊猫已不足只,属于中国国家一级保护动物。";
   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 imagefile = @"c:\users\administrator\pictures\.jpg";
   rectanglef rect = new rectanglef(, , , );
   presentation.slides[].shapes.appendembedimage(shapetype.rectangle, imagefile, rect);
   presentation.slides[].shapes[].line.fillformat.solidfillcolor.color = color.floralwhite;
 
   //添加形状
   rectanglef rect = new rectanglef(, , , );
   iautoshape shape = presentation.slides[].shapes.appendshape(shapetype.rectangle, rect);
   shape.fill.filltype = fillformattype.solid;
   shape.fill.filltype = fillformattype.none;
   shape.shapestyle.linecolor.color = color.white;
 
   //添加文本到形状中
   shape.textframe.text = "黑白相间的外表,有利隐蔽在密林的树上和积雪的地面而不易被天敌发现。相对锋利的爪和发达有力的前后肢,有利于大熊猫能快速爬上高大的乔木。";
   textrange textrange = shape.textframe.textrange;
 
   //设置文本字体
   textrange.fill.filltype = spire.presentation.drawing.fillformattype.solid;
   textrange.fill.solidcolor.color = color.blue;
   textrange.latinfont = new textfont("arial black");
 
   //保存文件
   presentation.savetofile(@"c:\users\administrator\desktop\result.pptx ", fileformat.pptx);
   system.diagnostics.process.start(@"c:\users\administrator\desktop\result.pptx ");
  }
 }
}

从上面的代码可以发现,其实通过这个组件,我们还可以自由地设置我们想要的形状、文本、字体、颜色等等,用起来确实方便又快速。感兴趣的话可以试一下其他丰富的效果。

导出图片

现在,我们导出上述运行后文档的图片。

具体步骤:

同样添加如下命名空间:

?
1
using spire.presentation;

步骤1: 新建一个presentation对象,并加载presentation文件。

?
1
2
presentation ppt = new presentation();
ppt.loadfromfile(@"c:\users\administrator\desktop\result.pptx");

 步骤2:遍历ppt文档所有的图片,并保存为.png格式。

?
1
2
3
4
5
6
7
8
9
for (int i = 0; i < ppt.images.count; i++)
 
   {
 
    image image = ppt.images[i].image;
 
    image.save(string.format(@"..\..\images{0}.png", i));
 
   }

效果图:

 C#向PPT文档插入图片以及导出图片的实例

全部代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using system;
using system.drawing;
using system.windows.forms;
using spire.presentation;
 
namespace extractimagesfromppt
{
 public partial class form : form
 {
  public form()
  {
   initializecomponent();
  }
 
  private void button_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{}.png", i));
 
   }
  }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/Yesi/p/6206470.html

延伸 · 阅读

精彩推荐
  • C#如何使用C#将Tensorflow训练的.pb文件用在生产环境详解

    如何使用C#将Tensorflow训练的.pb文件用在生产环境详解

    这篇文章主要给大家介绍了关于如何使用C#将Tensorflow训练的.pb文件用在生产环境的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴...

    bbird201811792022-03-05
  • C#VS2012 程序打包部署图文详解

    VS2012 程序打包部署图文详解

    VS2012虽然没有集成打包工具,但它为我们提供了下载的端口,需要我们手动安装一个插件InstallShield。网上有很多第三方的打包工具,但为什么偏要使用微软...

    张信秀7712021-12-15
  • C#C#微信公众号与订阅号接口开发示例代码

    C#微信公众号与订阅号接口开发示例代码

    这篇文章主要介绍了C#微信公众号与订阅号接口开发示例代码,结合实例形式简单分析了C#针对微信接口的调用与处理技巧,需要的朋友可以参考下...

    smartsmile20127762021-11-25
  • C#SQLite在C#中的安装与操作技巧

    SQLite在C#中的安装与操作技巧

    SQLite,是一款轻型的数据库,用于本地的数据储存。其优点有很多,下面通过本文给大家介绍SQLite在C#中的安装与操作技巧,感兴趣的的朋友参考下吧...

    蓝曈魅11162022-01-20
  • C#C#设计模式之Strategy策略模式解决007大破密码危机问题示例

    C#设计模式之Strategy策略模式解决007大破密码危机问题示例

    这篇文章主要介绍了C#设计模式之Strategy策略模式解决007大破密码危机问题,简单描述了策略模式的定义并结合加密解密算法实例分析了C#策略模式的具体使用...

    GhostRider10972022-01-21
  • C#深入理解C#的数组

    深入理解C#的数组

    本篇文章主要介绍了C#的数组,数组是一种数据结构,详细的介绍了数组的声明和访问等,有兴趣的可以了解一下。...

    佳园9492021-12-10
  • C#利用C#实现网络爬虫

    利用C#实现网络爬虫

    这篇文章主要介绍了利用C#实现网络爬虫,完整的介绍了C#实现网络爬虫详细过程,感兴趣的小伙伴们可以参考一下...

    C#教程网11852021-11-16
  • C#三十分钟快速掌握C# 6.0知识点

    三十分钟快速掌握C# 6.0知识点

    这篇文章主要介绍了C# 6.0的相关知识点,文中介绍的非常详细,通过这篇文字可以让大家在三十分钟内快速的掌握C# 6.0,需要的朋友可以参考借鉴,下面来...

    雨夜潇湘8272021-12-28