在製做Powerpoint文檔時,背景是很是重要的,統一的背景能讓Powerpoint 演示文稿看起來更加乾淨美觀。本文將詳細講述如何在Java應用程序中使用免費的Free Spire.Presentation for Java爲幻燈片設置純色背景顏色,漸變背景顏色以及添加背景圖片。html
Jar文件導入方法java
方法一:
下載最新的Free Spire.Presentation for Java包並解壓縮,而後從lib文件夾下,將Spire.Presentation.jar包導入到你的Java應用程序中。(導入成功後以下圖所示)app
方法二:
經過Maven倉庫安裝導入。詳細的操做步驟請參考連接:
https://www.e-iceblue.cn/licensing/install-spirepdf-for-java-from-maven-repository.htmlmaven
設置純色背景顏色ide
import com.spire.presentation.*; import com.spire.presentation.drawing.*; import java.awt.*; public class PPTbackground { public static void main(String[] args) throws Exception { //加載PowerPoint文檔 Presentation ppt = new Presentation(); ppt.loadFromFile("Sample.pptx"); //獲取幻燈片的數量 int slideCount = ppt.getSlides().getCount(); ISlide slide = null; //遍歷幻燈片,爲每張幻燈片設置純色背景色 for(int i = 0; i < slideCount;i++) { slide = ppt.getSlides().get(i); slide.getSlideBackground().setType(BackgroundType.CUSTOM); //設置純色背景填充 slide.getSlideBackground().getFill().setFillType(FillFormatType.SOLID); slide.getSlideBackground().getFill().getSolidColor().setColor(Color.lightGray); } //保存結果文檔 ppt.saveToFile("純色背景.pptx", FileFormat.PPTX_2010); } }
純色背景效果圖:3d
設置漸變背景顏色code
import com.spire.presentation.*; import com.spire.presentation.drawing.*; import java.awt.*; public class PPTbackground { public static void main(String[] args) throws Exception { //加載PowerPoint文檔 Presentation ppt = new Presentation(); ppt.loadFromFile("Sample.pptx"); //獲取幻燈片的數量 int slideCount = ppt.getSlides().getCount(); ISlide slide = null; //遍歷幻燈片,爲每張幻燈片設置漸變背景色 for(int i = 0; i < slideCount;i++) { slide = ppt.getSlides().get(i); slide.getSlideBackground().setType(BackgroundType.CUSTOM); //設置漸變背景色填充 slide.getSlideBackground().getFill().setFillType(FillFormatType.GRADIENT); slide.getSlideBackground().getFill().getGradient().getGradientStops().append(0, Color.WHITE); slide.getSlideBackground().getFill().getGradient().getGradientStops().append(1, Color.LIGHT_GRAY); } //保存結果文檔 ppt.saveToFile("漸變色背景.pptx", FileFormat.PPTX_2010); } }
漸變背景色效果圖:orm
**htm
添加背景圖片**blog
import com.spire.presentation.*; import com.spire.presentation.drawing.*; public class PPTbackground { public static void main(String[] args) throws Exception { //加載PowerPoint文檔 Presentation ppt = new Presentation(); ppt.loadFromFile("Sample.pptx"); //獲取幻燈片的數量 int slideCount = ppt.getSlides().getCount(); ISlide slide = null; //遍歷幻燈片,爲每張幻燈片添加背景圖片 for(int i = 0; i < slideCount;i++) { slide = ppt.getSlides().get(i); slide.getSlideBackground().setType(BackgroundType.CUSTOM); //設置圖片背景填充 slide.getSlideBackground().getFill().setFillType(FillFormatType.PICTURE); slide.getSlideBackground().getFill().getPictureFill().setAlignment(RectangleAlignment.NONE); slide.getSlideBackground().getFill().getPictureFill().setFillType(PictureFillType.STRETCH); slide.getSlideBackground().getFill().getPictureFill().getPicture().setUrl((new java.io.File("1.png")).getAbsolutePath()); } //保存結果文檔 ppt.saveToFile("背景圖片.pptx", FileFormat.PPTX_2010); } }
添加背景圖效果: