制作一个精美的PPT文档,不仅要求内容充实、排版得当;同时对于背景颜色的搭配,尤其是背景图片的设置也尤为重要。恰当的背景颜色或图片能够使PPT更加美观,引人注目。本文就将通过使用Java程序来演示如何给PPT幻灯片添加背景颜色和背景图片。背景颜色主要分为纯色背景颜色和渐变色背景颜色。
使用工具:Free Spire.Presentation for Java(免费版)
Jar文件获取及导入:
方法1:通过E-iceblue中文官网下载获取jar包。解压后将lib文件夹下的Spire.Presentation.jar文件导入Java程序。(如下图)
方法2:通过maven仓库安装导入。具体安装教程详见E-iceblue中文官网。
【示例1】添加背景图片
import com.spire.presentation.*;import com.spire.presentation.drawing.*;public class BackgroundImage { public static void main(String[] args) throws Exception { String inputFile = "C:甥敳獲Test1DesktopSample.pptx"; String imageFile = "C:甥敳獲Test1DesktopImage.jpg"; String outputFile = "output/setBackgroundImage.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(); }}
背景图片添加效果:
【示例2】添加背景颜色
Part 1:添加纯色背景颜色
import com.spire.presentation.*;import com.spire.presentation.drawing.*;public class PureBackgroundColor { public static void main(String[] args) throws Exception { String inputFile = "C:甥敳獲Test1DesktopSample.pptx"; String outputFile = "output/PureBackgroundColor.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.LIGHT_GRAY); ppt.saveToFile(outputFile, FileFormat.PPTX_2010); ppt.dispose(); }}
纯色背景颜色添加效果:
Part 2:添加渐变色背景颜色
渐变色背景颜色添加效果:
(本文完)