我們?cè)趧?chuàng)建powerpoint文檔時(shí),系統(tǒng)默認(rèn)的幻燈片是空白背景的,很多時(shí)候我們需要自定義幻燈片背景,以達(dá)到美觀的文檔效果。在下面的示例中將介紹給powerpoint幻燈片設(shè)置背景的方法,主要包含以下三個(gè)部分:
- 添加純色背景
- 添加漸變色背景
- 添加圖片作為背景
所需工具
free spire.presentation for .net 版本3.3 (社區(qū)版)
示例代碼(供參考)
步驟 1 :添加如下using指令
1
2
3
|
using spire.presentation; using spire.presentation.drawing; using system.drawing; |
步驟 2 :創(chuàng)建文檔
1
2
|
presentation ppt = new presentation(); ppt.loadfromfile( "test.pptx" ); |
步驟 3 :添加純色背景
1
2
3
4
|
//設(shè)置文檔的背景填充模式為純色填充 ppt.slides[0].slidebackground.type = backgroundtype.custom; ppt.slides[0].slidebackground.fill.filltype = fillformattype.solid; ppt.slides[0].slidebackground.fill.solidcolor.color = color.pink; |
步驟 4 :添加漸變背景色
1
2
3
4
5
|
//設(shè)置文檔的背景填充模式為漸變色填充 ppt.slides[1].slidebackground.type = backgroundtype.custom; ppt.slides[1].slidebackground.fill.filltype = fillformattype.gradient; ppt.slides[1].slidebackground.fill.gradient.gradientstops.append(0f, knowncolors.yellow); ppt.slides[1].slidebackground.fill.gradient.gradientstops.append(1f, knowncolors.orange); |
步驟 5 :添加圖片作為背景
1
2
3
4
5
6
7
8
|
//設(shè)置幻燈片背景色為圖片背景 ppt.slides[2].slidebackground.type = spire.presentation.drawing.backgroundtype.custom; ppt.slides[2].slidebackground.fill.filltype = fillformattype.picture; ppt.slides[2].slidebackground.fill.picturefill.filltype = picturefilltype.stretch; //加載圖片作為幻燈片背景 image img = image.fromfile( "green.png" ); iimagedata image = ppt.images.append(img); ppt.slides[2].slidebackground.fill.picturefill.picture.embedimage = image; |
步驟6 :保存文件
1
2
|
ppt.savetofile( "result.pptx" , fileformat.pptx2010); system.diagnostics.process.start( "result.pptx" ); |
完成代碼后,調(diào)試運(yùn)行程序,生成文件,如下:
全部代碼:
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
|
using spire.presentation; using spire.presentation.drawing; using system.drawing; namespace addbackground_ppt { class program { static void main( string [] args) { //實(shí)例化presentation類,加載powerpoint文檔 presentation ppt = new presentation(); ppt.loadfromfile( "test.pptx" ); //設(shè)置文檔的背景填充模式為純色填充 ppt.slides[0].slidebackground.type = backgroundtype.custom; ppt.slides[0].slidebackground.fill.filltype = fillformattype.solid; ppt.slides[0].slidebackground.fill.solidcolor.color = color.pink; //設(shè)置文檔的背景填充模式為漸變色填充 ppt.slides[1].slidebackground.type = backgroundtype.custom; ppt.slides[1].slidebackground.fill.filltype = fillformattype.gradient; ppt.slides[1].slidebackground.fill.gradient.gradientstops.append(0f, knowncolors.yellow); ppt.slides[1].slidebackground.fill.gradient.gradientstops.append(1f, knowncolors.orange); //設(shè)置幻燈片背景色為圖片背景 ppt.slides[2].slidebackground.type = spire.presentation.drawing.backgroundtype.custom; ppt.slides[2].slidebackground.fill.filltype = fillformattype.picture; ppt.slides[2].slidebackground.fill.picturefill.filltype = picturefilltype.stretch; //加載圖片作為幻燈片背景 image img = image.fromfile( "green.png" ); iimagedata image = ppt.images.append(img); ppt.slides[2].slidebackground.fill.picturefill.picture.embedimage = image; //保存并打開(kāi)文檔 ppt.savetofile( "result.pptx" , fileformat.pptx2010); system.diagnostics.process.start( "result.pptx" ); } } } |
本文完。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.cnblogs.com/Yesi/archive/2018/07/26/9369849.html