ppt幻燈片生成時,系統默認是無色背景填充,幻燈片設計需要手動設置背景效果,可設置顏色填充或者圖片背景填充。本文將對此介紹具體實現方法。
jar文件導入方法(參考):
步驟1:在java程序中可新建一個文件夾命名為lib,并將下載包中的jar文件復制到新建的文件夾下。
步驟2:復制文件后,添加到引用類庫:選中這個jar文件,點擊鼠標右鍵,選擇“build path” – “add to build path”。完成引用。
java示例1:設置背景顏色
1.純色背景
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import com.spire.presentation.*; import com.spire.presentation.drawing.*; public class backgroundcolor { public static void main(string[] args) throws exception { string inputfile = "sample.pptx" ; string outputfile = "output/setbackgroundcolor.pptx" ; presentation ppt = new presentation(); ppt.loadfromfile(inputfile); ppt.getslides().get( 0 ).getslidebackground().settype(backgroundtype.custom); //設置文檔的背景填充模式為純色填充,設置顏色 ppt.getslides().get( 0 ).getslidebackground().getfill().setfilltype(fillformattype.solid); ppt.getslides().get( 0 ).getslidebackground().getfill().getsolidcolor().setcolor(java.awt.color.pink); ppt.savetofile(outputfile, fileformat.pptx_2010); ppt.dispose(); } } |
純色背景效果:
2.漸變背景
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import java.awt.color; import com.spire.presentation.*; import com.spire.presentation.drawing.*; public class backgroundcolor { public static void main(string[] args) throws exception { string inputfile = "test.pptx" ; string outputfile = "output/setbackgroundcolor2.pptx" ; presentation ppt = new presentation(); ppt.loadfromfile(inputfile); ppt.getslides().get( 0 ).getslidebackground().settype(backgroundtype.custom); //設置文檔的背景填充模式為漸變填充,并設置顏色 ppt.getslides().get( 0 ).getslidebackground().getfill().setfilltype(fillformattype.gradient); ppt.getslides().get( 0 ).getslidebackground().getfill().getgradient().getgradientstops().append( 0 , color.white); ppt.getslides().get( 0 ).getslidebackground().getfill().getgradient().getgradientstops().append( 1 ,color.green); ppt.savetofile(outputfile, fileformat.pptx_2010); ppt.dispose(); } } |
漸變色背景效果:
java示例2:圖片背景
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import com.spire.presentation.*; import com.spire.presentation.drawing.*; public class imagebackground { public static void main(string[] args) throws exception { string inputfile = "input.pptx" ; string imagefile = "1.png" ; string outputfile = "output/imgbackgroundcolor.pptx" ; presentation ppt = new presentation(); ppt.loadfromfile(inputfile); ppt.getslides().get( 0 ).getslidebackground().settype(backgroundtype.custom); //設置文檔的背景填充模式為圖片填充 ppt.getslides().get( 0 ).getslidebackground().getfill().setfilltype(fillformattype.picture); ppt.getslides().get( 0 ).getslidebackground().getfill().getpicturefill().setalignment(rectanglealignment.none); ppt.getslides().get( 0 ).getslidebackground().getfill().getpicturefill().setfilltype(picturefilltype.stretch); ppt.getslides().get( 0 ).getslidebackground().getfill().getpicturefill().getpicture().seturl(( new java.io.file(imagefile)).getabsolutepath()); ppt.savetofile(outputfile, fileformat.pptx_2010); ppt.dispose(); } } |
圖片背景效果:
![]() |
以上所述是小編給大家介紹的java設置ppt幻燈片背景——純色、漸變、圖片背景詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://www.imooc.com/article/282027