PowerPoint 演示文稿的背景可以设定演示文稿的基调和情绪,同时极大地提高幻灯片的美观度和感染力。PowerPoint 幻灯片有五种类型的背景可用,包括纯色背景、渐变背景、图片背景、纹理背景和图案背景。这些背景分别适用于不同的使用场景。例如,专业的商业演示文稿可能会受益于简洁明了的纯色背景,而创意演示文稿则可能会使用具有启发性和趣味性的图片背景来吸引观众的注意力。本文将展示如何使用 Spire.Presentation for Java 通过 Java 程序来设置 PowerPoint 演示文稿的背景。
- 给 PowerPoint 幻灯片设置纯色背景
- 给 PowerPoint 幻灯片设置渐变背景
- 给 PowerPoint 幻灯片设置图片背景
- 给 PowerPoint 幻灯片设置纹理背景
- 给 PowerPoint 幻灯片设置图案背景
安装 Spire.Presentation for Java
首先,您需要在 Java 程序中添加 Spire.Presentation.jar 文件作为依赖项。JAR 文件可以从此链接下载。如果您使用 Maven,则可以将以下代码添加到项目的 pom.xml 文件中,从而轻松地在应用程序中导入 JAR 文件。
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.presentation</artifactId>
        <version>10.10.2</version>
    </dependency>
</dependencies>
给 PowerPoint 幻灯片设置纯色背景
在自定义幻灯片背景之前,需要使用 SlideBackground.setType(BackgroundType.CUSTOM) 方法来允许自定义背景。然后,使用 SlideBackground.getFill().setFillType(FillFormatType.SOLID) 方法将背景类型设置为纯色背景,并使用 FileFormat.getSolidColor().setColor() 方法设置背景颜色。
详细操作步骤如下:
- 创建 Presentation 类的对象。
- 使用 Presentation.loadFromFile() 方法载入演示文稿。
- 使用 Presentation.getSlides().get() 方法获取第一张幻灯片。
- 使用 ISlide.getSlideBackground() 方法获取幻灯片的背景。
- 使用 SlideBackground.setType(BackgroundType.CUSTOM) 方法将背景类型设置为自定义,以允许自定义背景。
- 使用 SlideBackground.getFill().setFillType(FillFormatType.SOLID) 方法将背景填充类型设置为纯色。
- 使用 FileFormat.getSolidColor().setColor() 方法自定义背景颜色。
- 使用 Presentation.saveToFile() 方法保存演示文稿。
- Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import com.spire.presentation.SlideBackground;
import com.spire.presentation.drawing.BackgroundType;
import com.spire.presentation.drawing.FillFormat;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
public class SolidColor {
    public static void main(String[] args) throws Exception {
        //创建一个Presentation类的对象
        Presentation ppt = new Presentation();
        //载入PowerPoint演示文稿
        ppt.loadFromFile("示例.pptx");
        //获取第一张幻灯片
        ISlide slide = ppt.getSlides().get(0);
        //获取幻灯片背景
        SlideBackground background = slide.getSlideBackground();
        //将背景类型设置为自定义
        background.setType(BackgroundType.CUSTOM);
        //将背景填充类型设置为纯色
        background.getFill().setFillType(FillFormatType.SOLID);
        //设置背景颜色
        FillFormat fillFormat = background.getFill();
        fillFormat.getSolidColor().setColor(new Color(199, 213, 237));
        //保存演示文稿
        ppt.saveToFile("纯色背景.pptx", FileFormat.AUTO);
    }
}
给 PowerPoint 幻灯片设置渐变背景
渐变背景可以通过将背景类型设置为渐变背景,然后设置渐变类型、停止点、颜色和角度来完成设置。具体步骤如下:
- 创建一个 Presentation 类的对象。
- 使用 Presentation.loadFromFile() 方法载入演示文稿。
- 使用 Presentation.getSlides().get() 方法获取第一张幻灯片。
- 使用 ISlide.getSlideBackground() 方法获取幻灯片的背景。
- 使用 SlideBackground.setType(BackgroundType.CUSTOM) 方法将背景类型设置为自定义,以允许自定义背景。
- 使用 SlideBackground.getFill().setFillType(FillFormatType.GRADIENT) 方法将背景的填充类型设置为渐变。
- 使用 GradientFillFormat.setGradientShape(GradientShapeType.LINEAR) 方法将渐变类型设置为线性渐变。
- 使用 GradientFillFormat.getGradientStops().append() 方法添加渐变停止点并设置渐变颜色。
- 使用 GradientFillFormat.getLinearGradientFill().setAngle() 方法设置线性渐变的角度。
- 使用 Presentation.saveToFile() 方法保存演示文稿。
- Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import com.spire.presentation.SlideBackground;
import com.spire.presentation.drawing.*;
import java.awt.*;
public class Gradient {
    public static void main(String[] args) throws Exception {
        //创建一个Presentation类的对象
        Presentation ppt = new Presentation();
        //载入PowerPoint演示文稿
        ppt.loadFromFile("C:/Users/Sirion/Desktop/示例.pptx");
        //获取第一张幻灯片
        ISlide slide = ppt.getSlides().get(0);
        //获取幻灯片背景
        SlideBackground background = slide.getSlideBackground();
        //将背景类型设置为自定义
        background.setType(BackgroundType.CUSTOM);
        //将背景填充类型设置为渐变
        background.getFill().setFillType(FillFormatType.GRADIENT);
        //将渐变类型设置为线性渐变
        GradientFillFormat gradient = background.getFill().getGradient();
        gradient.setGradientShape(GradientShapeType.LINEAR);
        //添加渐变停止点并设置颜色
        gradient.getGradientStops().append(0f, new Color(230, 255, 255));
        gradient.getGradientStops().append(0.5f, new Color(255, 255, 255));
        gradient.getGradientStops().append(1f, new Color(199, 213, 237));
        //设置渐变角度
        gradient.getLinearGradientFill().setAngle(90);
        //保存演示文稿
        ppt.saveToFile("渐变背景.pptx", FileFormat.AUTO);
    }
}
给 PowerPoint 幻灯片设置图片背景
在给幻灯片设置图片背景时,需要将背景类型设置为图片,然后将图片填充类型设置为拉伸填充,最后再设置背景图片。具体步骤如下:
- 创建一个 Presentation 类的对象。
- 使用 Presentation.loadFromFile() 方法载入演示文稿。
- 使用 Presentation.getImages().append() 方法载入图片。
- 使用 Presentation.getSlides().get() 方法获取第一张幻灯片。
- 使用 ISlide.getSlideBackground() 方法获取幻灯片的背景。
- 使用 SlideBackground.setType(BackgroundType.CUSTOM) 方法将背景类型设置为自定义,以允许自定义背景。
- 使用 SlideBackground.getFill().setFillType(FillFormatType.PICTURE) 方法将背景的填充类型设置为图片。
- 使用 PictureFillFormat.setFillType(PictureFillType.STRETCH) 方法将图片填充方式设置为拉伸填充。
- 使用 PictureFillFormat.getPicture().setTransparency() 方法设置图片背景透明度。
- 使用 PictureFillFormat.getPicture().setEmbedImage() 方法设置背景图片。
- 使用 Presentation.saveToFile() 方法保存演示文稿。
- Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import com.spire.presentation.SlideBackground;
import com.spire.presentation.drawing.*;
import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
public class Picture {
    public static void main(String[] args) throws Exception {
        //创建一个Presentation类的对象
        Presentation ppt = new Presentation();
        //载入PowerPoint演示文稿
        ppt.loadFromFile("示例.pptx");
        //载入图片
        IImageData image = ppt.getImages().append(ImageIO.read(new File("背景.jpg")));
        //获取第一张幻灯片
        ISlide slide = ppt.getSlides().get(0);
        //获取幻灯片背景
        SlideBackground background = slide.getSlideBackground();
        //将背景类型设置为自定义
        background.setType(BackgroundType.CUSTOM);
        //将背景填充类型设置为图片
        background.getFill().setFillType(FillFormatType.PICTURE);
        //将图片填充方式设置为拉伸填充
        PictureFillFormat pictureFillFormat = background.getFill().getPictureFill();
        pictureFillFormat.setFillType(PictureFillType.STRETCH);
        //设置图片背景透明度
        pictureFillFormat.getPicture().setTransparency(50);
        //设置背景图片
        pictureFillFormat.getPicture().setEmbedImage(image);
        //保存演示文稿
        ppt.saveToFile("图片背景.pptx", FileFormat.AUTO);
    }
}
给 PowerPoint 幻灯片设置纹理背景
设置纹理背景与设置图片背景的操作步骤类似。不同之处在于,图片填充类型需要更改为平铺填充,并可以对纹理对齐方式进行设置。具体步骤如下:
- 创建一个 Presentation 类的对象。
- 使用 Presentation.loadFromFile() 方法载入演示文稿。
- 使用 Presentation.getImages().append() 方法载入纹理图片。
- 使用 Presentation.getSlides().get() 方法获取第一张幻灯片。
- 使用 ISlide.getSlideBackground() 方法获取幻灯片的背景。
- 使用 SlideBackground.setType(BackgroundType.CUSTOM) 方法将背景类型设置为自定义,以允许自定义背景。
- 使用 SlideBackground.getFill().setFillType(FillFormatType.PICTURE) 方法将背景的填充类型设置为图片。
- 使用 PictureFillFormat.setFillType(PictureFillType.TILE) 方法将图片填充方式设置为平铺填充。
- 使用 PictureFillFormat.setAlignment() 方法设置纹理图片的对齐方式。
- 使用 PictureFillFormat.getPicture().setTransparency() 方法设置纹理背景透明度。
- 使用 PictureFillFormat.getPicture().setEmbedImage() 方法设置背景纹理。
- 使用 Presentation.saveToFile() 方法保存演示文稿。
- Java
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import javax.imageio.ImageIO;
import java.io.File;
public class Texture {
    public static void main(String[] args) throws Exception {
        //创建一个Presentation类的对象
        Presentation ppt = new Presentation();
        //载入PowerPoint演示文稿
        ppt.loadFromFile("示例.pptx");
        //载入纹理图片
        IImageData image = ppt.getImages().append(ImageIO.read(new File("纹理.png")));
        //获取第一张幻灯片
        ISlide slide = ppt.getSlides().get(0);
        //获取幻灯片背景
        SlideBackground background = slide.getSlideBackground();
        //将背景类型设置为自定义
        background.setType(BackgroundType.CUSTOM);
        //将背景填充类型设置为图片
        background.getFill().setFillType(FillFormatType.PICTURE);
        //将图片填充类型设置为平铺
        PictureFillFormat pictureFillFormat = background.getFill().getPictureFill();
        pictureFillFormat.setFillType(PictureFillType.TILE);
        //设置纹理对齐方式
        pictureFillFormat.setAlignment(RectangleAlignment.TOP_LEFT);
        //设置纹理背景的透明度
        pictureFillFormat.getPicture().setTransparency(50);
        //设置背景纹理
        pictureFillFormat.getPicture().setEmbedImage(image);
        //保存演示文稿
        ppt.saveToFile("纹理背景.pptx", FileFormat.AUTO);
    }
}
给 PowerPoint 幻灯片设置图案背景
在设置图案背景时,需要设置图案类型以及图案的前景颜色和背景颜色。详细操作步骤如下:
- 创建一个 Presentation 类的对象。
- 使用 Presentation.loadFromFile() 方法载入演示文稿。
- 使用 Presentation.getSlides().get() 方法获取第一张幻灯片。
- 使用 ISlide.getSlideBackground() 方法获取幻灯片的背景。
- 使用 SlideBackground.setType(BackgroundType.CUSTOM) 方法将背景类型设置为自定义,以允许对背景进行自定义。
- 使用 SlideBackground.getFill().setFillType(FillFormatType.PATTERN) 方法将背景的填充类型设置为图案。
- 使用 PatternFillFormat.setPatternType() 方法设置图案类型。
- 使用 PatternFillFormat.getForegroundColor().setColor() 方法设置图案的前景颜色。
- 使用 PatternFillFormat.getBackgroundColor().setColor() 方法设置图案的背景颜色。
- 使用 Presentation.saveToFile() 方法保存演示文稿。
- Java
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
public class Pattern {
    public static void main(String[] args) throws Exception {
        //创建一个Presentation类的对象
        Presentation ppt = new Presentation();
        //载入PowerPoint演示文稿
        ppt.loadFromFile("示例.pptx");
        //获取第一张幻灯片
        ISlide slide = ppt.getSlides().get(0);
        //获取幻灯片背景
        SlideBackground background = slide.getSlideBackground();
        //将背景类型设置为自定义
        background.setType(BackgroundType.CUSTOM);
        //将背景填充类型设置为图案
        background.getFill().setFillType(FillFormatType.PATTERN);
        //设置图案类型
        PatternFillFormat patternFillFormat = background.getFill().getPattern();
        patternFillFormat.setPatternType(PatternFillType.DOTTED_GRID);
        //设置图案的前景颜色
        patternFillFormat.getForegroundColor().setColor(new Color(230, 255, 255));
        //设置图案的背景颜色
        patternFillFormat.getBackgroundColor().setColor(new Color(199, 213, 237));
        //保存演示文稿
        ppt.saveToFile("图案背景.pptx", FileFormat.AUTO);
    }
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
 



 
					



