Free Spire.Presentation for Java (免费版)
步骤1:在Java程序中新建一个文件夹可命名为Lib。并将下载包中的jar文件复制到新建的文件夹下。
import java.awt.Color;import com.spire.presentation.*;import com.spire.presentation.drawing.*;public class PPTbackground { 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(); }}
纯色效果:
import java.awt.Color;import com.spire.presentation.*;import com.spire.presentation.drawing.*;public class PPTbackground { 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(); }}
渐变效果: