将背景图像应用于Java中的所有幻灯片

Free Spire.Presentation for Java provides a SlideBackground class to work with the background of a slide. By using it, you’re able to set a slide background with a solid fill, a gradient fill, a picture fill or a pattern fill.

在本文中,您将学习将背景图像应用于现有PowerPoint演示文稿中的所有幻灯片的两种方法。 第一种方法是使用for循环为每张幻灯片设置背景,第二种方法是使用主幻灯片。 母版幻灯片不仅可以保留背景,还可以保留要在所有幻灯片上显示的图像(例如公司徽标)或文本。

这是输入PowerPoint文件的外观。

Format text content

Method 1. Use a for loop

import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
import com.spire.presentation.SlideBackground;
import com.spire.presentation.drawing.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;

public class AppplyBgToAllSlides {

    public static void main(String[] args) throws Exception {

        //load a PowerPoint file
        Presentation presentation = new Presentation();
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx");

        //get the image data
        BufferedImage image = ImageIO.read(new FileInputStream("C:\\Users\\Administrator\\Desktop\\bg.jpg"));
        IImageData imageData = presentation.getImages().append(image);

        //loop through the slides
        for (int i = 0; i < presentation.getSlides().getCount() ; i++) {

            //apply the image to the certain slide as background
            SlideBackground background = presentation.getSlides().get(i).getSlideBackground();
            background.setType(BackgroundType.CUSTOM);
            background.getFill().setFillType(FillFormatType.PICTURE);
            background.getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
            background.getFill().getPictureFill().getPicture().setEmbedImage(imageData);
        }

        //save to file
        presentation.saveToFile("output.pptx", FileFormat.PPTX_2013);
        presentation.dispose();
    }
}

Format text content

Method 2. Use master slide

import com.spire.presentation.*;
import com.spire.presentation.drawing.BackgroundType;
import com.spire.presentation.drawing.FillFormatType;
import com.spire.presentation.drawing.IImageData;
import com.spire.presentation.drawing.PictureFillType;

import javax.imageio.ImageIO;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;


public class ApplySlideMaster {

    public static void main(String[] args) throws Exception  {

        //load a PowerPoint file
        Presentation presentation = new Presentation();
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx");

        //get the first slide master
        IMasterSlide masterSlide = presentation.getMasters().get(0);

        //set the background image of the slide master
        String backgroundPic = "C:/Users/Administrator/Desktop/bg.jpg";
        BufferedImage image = ImageIO.read(new FileInputStream(backgroundPic));
        IImageData imageData = presentation.getImages().append(image);
        masterSlide.getSlideBackground().setType(BackgroundType.CUSTOM);
        masterSlide.getSlideBackground().getFill().setFillType(FillFormatType.PICTURE);
        masterSlide.getSlideBackground().getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
        masterSlide.getSlideBackground().getFill().getPictureFill().getPicture().setEmbedImage(imageData);

        //add an image (company logo) to the slide master
        String logo = "C:/Users/Administrator/Desktop/logo.png";
        image = ImageIO.read(new FileInputStream(logo));
        imageData = presentation.getImages().append(image);
        IEmbedImage imageShape = masterSlide.getShapes().appendEmbedImage(ShapeType.RECTANGLE,imageData,new Rectangle2D.Float(40,40,200,60));
        imageShape.getLine().setFillType(FillFormatType.NONE);

        //save to file
        presentation.saveToFile("output.pptx", FileFormat.PPTX_2013);
        presentation.dispose();
    }
}

Format text content

from: https://dev.to//eiceblue/apply-background-image-to-all-slides-in-java-58mf

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值