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 ...
随机推荐
- 【.net 深呼吸】细说CodeDom(1):结构大观
CodeDom 是啥东东?Html Dom听过吧,XML Dom听过吧.DOM一般可翻译为 文档对象模型,那 Code + DOM呢,自然是指代码文档模型了.如果你从来没接触过 CodeDom,你大概 ...
- 恢复SQL Server被误删除的数据(再扩展)
恢复SQL Server被误删除的数据(再扩展) 大家对本人之前的文章<恢复SQL Server被误删除的数据> 反应非常热烈,但是文章里的存储过程不能实现对备份出来的日志备份里所删数据的 ...
- Nexus(一)环境搭建
昨天,成功搭建了自己的 Maven 环境(详见:Maven(一)环境搭建),今天就来研究和探讨下 Nexus 的搭建! 使用背景: 安装环境:Windows 10 -64位 JDK版本:1.7 Mav ...
- StrategyPattern (策略模式)
/** * 策略模式 * @author TMAC-J * 根据环境的不同选择不同的策略,把策略用接口抽象出来 */ public class StrategyPattern { interface ...
- 如何解决流程开发中SheetRadioButtonList页面取值问题
分享一个常见的取值问题. 应用场景: SheetRadioButtonList控件,点击其中一项执行事件操作.如果是页面加载的情况下,值就无法取到. 具体原因如下: 我给SheetRadioButto ...
- Android(安卓)-------CardView
1.activity_main.xml <android.support.v7.widget.CardView android:id="@+id/cardView" andr ...
- CYQ.Data V5 从入门到放弃ORM系列:教程 - MAction类使用
背景: 随着V5框架使用者的快速增加,终于促使我开始对整个框架编写完整的Demo. 上周大概花了一星期的时间,每天写到夜里3点半,终完成了框架所有功能的Demo. 同时,按V5框架名称空间的顺序,对每 ...
- mono for android 用ISharedPreferences 进行状态保持 会话保持 应用程序首选项保存
由于项目需要 要保持用户登录状态 要进行状态保持 用途就好像asp.net的session一样 登录的时候进行保存 ISharedPreferences shared = GetSharedPrefe ...
- 小小改动帮你减少bundle.js文件体积(翻译)
我已经从事过好多年的SPA开发工作,我发现很多的程序猿都从来不往 bundle.js 文件的体积上动脑筋,这让我有点懵逼. “安心洗路,等俺把代码混淆压缩后就一切666了”,若是有人这么说,我会翻白眼 ...
- Goodbye2014,Hello2015
正如我在研发会议上说的,总结是为了更好的计划:而计划,则是让你做事有目标,有方向:有了目标和方向,你才能真正把事情做成! 总的来说2014年可以归纳为下图: 2014年总结 一年的活动,基本可以归纳为 ...