制做一个精美的PPT文档,不只要求内容充实、排版得当;同时对于背景颜色的搭配,尤为是背景图片的设置也尤其重要。恰当的背景颜色或图片可以使PPT更加美观,引人注目。本文就将经过使用Java程序来演示如何给PPT幻灯片添加背景颜色和背景图片。背景颜色主要分为纯色背景颜色和渐变色背景颜色。html
方法1:经过官方网站下载获取jar包。解压后将lib文件夹下的Spire.Presentation.jar文件导入Java程序。(以下图)java
方法2:经过maven仓库安装导入。具体安装教程详见此网页。app
import com.spire.presentation.*; import com.spire.presentation.drawing.*; public class BackgroundImage { public static void main(String[] args) throws Exception { String inputFile = "C:\\Users\\Test1\\Desktop\\Sample.pptx"; String imageFile = "C:\\Users\\Test1\\Desktop\\Image.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(); } }
背景图片添加效果:maven
Part 1:添加纯色背景颜色ide
import com.spire.presentation.*; import com.spire.presentation.drawing.*; public class PureBackgroundColor { public static void main(String[] args) throws Exception { String inputFile = "C:\\Users\\Test1\\Desktop\\Sample.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: 添加渐变色背景颜色网站
import com.spire.presentation.*; import com.spire.presentation.drawing.*; import java.awt.*; public class GradientColor { public static void main(String[] args) throws Exception { String inputFile = "C:\\Users\\Test1\\Desktop\\Sample.pptx"; String outputFile = "output/setGradientColor.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.darkGray); ppt.saveToFile(outputFile, FileFormat.PPTX\_2010); ppt.dispose(); } }
渐变色背景颜色添加效果:spa
(本文完)code