该示例将详细讲述如何使用Spire.Presentation for Java在Java应用程序中为幻灯片设置纯色背景颜色,渐变背景颜色以及添加背景图片。
设置背景图片
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 imageFile = "1.png";
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.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();
}
}
设置纯色背景颜色
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.LIGHT_GRAY);
ppt.saveToFile(outputFile, FileFormat.PPTX_2010);
ppt.dispose();
}
}
设置渐变色背景颜色
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import java.awt.*;
public class PPTbackground {
public static void main(String[] args) throws Exception {
String inputFile = "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.green);
ppt.saveToFile(outputFile, FileFormat.PPTX_2010);
ppt.dispose();
}
}