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(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.Pptx2010);
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 {
9 public partial class Form1 : Form
10 {
11 public Form1()
12 {
13 InitializeComponent();
14 }
15
16 private void button1_Click(object sender, EventArgs e)
17 {
18 //建立PPT
19 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.Pptx2010);
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 {
8 public 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 }
謝謝浏覽!