1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > C#向PPT文档插入图片以及导出图片

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

时间:2024-07-01 08:50:45

相关推荐

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

PowerPoint演示文稿是我们日常工作中常用的办公软件之一,而图片则是PowerPoint文档的重要组成部分,那么如何向幻灯片插入图片以及导出图片呢?本文我将给大家分享如何使用一个免费版PowerPoint组件—Free Spire.Presentation,以C#/编程的方式来快速地实现这两个功能。我们可以从官网下载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(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添加形状,再添加文本到形状里面。

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同样,插入第二张图片到第二张幻灯片,添加形状,再添加文本到形状里面。最后保存文档。

presentation.SaveToFile(@"C:\Users\Administrator\Desktop\result.pptx ", FileFormat.Pptx);System.Diagnostics.Process.Start(@"C:\Users\Administrator\Desktop\result.pptx ");

效果图:

全部代码:

1 using System; 2 using System.Drawing; 3 using System.Windows.Forms; 4 using Spire.Presentation; 5 using Spire.Presentation.Drawing; 6 7 namespace InsertimageinPowerPointFille 8 { 9public partial class Form1 : Form10{11 public Form1()12 {13 InitializeComponent();14 }15 16 private void button1_Click(object sender, EventArgs e)17 {18 //新建PPT19 Presentation presentation = new Presentation();20 presentation.Slides.Append();21 22 //插入第一张图片到第一张幻灯片23 string ImageFile = @"C:\Users\Administrator\Pictures\01.jpg";24 RectangleF rect = new RectangleF(350, 100, 300, 250);25 presentation.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect);26 presentation.Slides[0].Shapes[0].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite;27 28 //添加形状29 RectangleF rect2 = new RectangleF(50, 100, 300, 250);30 IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, rect2);31 shape.Fill.FillType = FillFormatType.None;32 shape.ShapeStyle.LineColor.Color = Color.White;33 34 //添加文本到形状中35 shape.TextFrame.Text = "大熊猫是哺乳动物,已在地球上生存了至少800万年,被誉为活化石和中国国宝,世界自然基金会的形象大使,是世界生物多样性保护的旗舰物种。据第三次全国大熊猫野外种群调查,全世界野生大熊猫已不足1600只,属于中国国家一级保护动物。";36 TextRange textRange = shape.TextFrame.TextRange;37 shape.TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Left;38 39 //设置文本字体40 textRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;41 textRange.Fill.SolidColor.Color = Color.Black;42 textRange.LatinFont = new TextFont("Arial Black");43 44 //插入第二张图片到第二张幻灯片45 string ImageFile1 = @"C:\Users\Administrator\Pictures\02.jpg";46 RectangleF rect1 = new RectangleF(80, 100, 500, 400);47 presentation.Slides[1].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile1, rect1);48 presentation.Slides[1].Shapes[0].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite;49 50 //添加形状51 RectangleF rect3 = new RectangleF(30, 40, 630, 50);52 IAutoShape shape1 = presentation.Slides[1].Shapes.AppendShape(ShapeType.Rectangle, rect3);53 shape1.Fill.FillType = FillFormatType.Solid;54 shape1.Fill.FillType = FillFormatType.None;55 shape1.ShapeStyle.LineColor.Color = Color.White;56 57 //添加文本到形状中58 shape1.TextFrame.Text = "黑白相间的外表,有利隐蔽在密林的树上和积雪的地面而不易被天敌发现。相对锋利的爪和发达有力的前后肢,有利于大熊猫能快速爬上高大的乔木。";59 TextRange textRange1 = shape1.TextFrame.TextRange;60 61 //设置文本字体62 textRange1.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;63 textRange1.Fill.SolidColor.Color = Color.Blue;64 textRange1.LatinFont = new TextFont("Arial Black");65 66 //保存文件67 presentation.SaveToFile(@"C:\Users\Administrator\Desktop\result.pptx ", FileFormat.Pptx);68 System.Diagnostics.Process.Start(@"C:\Users\Administrator\Desktop\result.pptx ");69 }70}71 }

View Code

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

导出图片

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

具体步骤:

同样添加如下命名空间:

using Spire.Presentation;

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

Presentation ppt = new Presentation();ppt.LoadFromFile(@"C:\Users\Administrator\Desktop\result.pptx");

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

for (int i = 0; i < ppt.Images.Count; i++){Image image = ppt.Images[i].Image;image.Save(string.Format(@"..\..\Images{0}.png", i));}

效果图:

全部代码:

1 using System; 2 using System.Drawing; 3 using System.Windows.Forms; 4 using Spire.Presentation; 5 6 namespace ExtractImagesfromPPT 7 { 8public partial class Form1 : Form 9{10 public Form1()11 {12 InitializeComponent();13 }14 15 private void button1_Click(object sender, EventArgs e)16 {17 Presentation ppt = new Presentation();18 ppt.LoadFromFile(@"C:\Users\Administrator\Desktop\result.pptx");19 for (int i = 0; i < ppt.Images.Count; i++)20 {21 Image image = ppt.Images[i].Image;22 image.Save(string.Format(@"..\..\Images{0}.png", i));23 24 }25 }26}27 }

View Code

谢谢浏览!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。