Мастер слайдов в PowerPoint сохраняет фиксированные стили, такие как фон, заголовок, цвет темы и т.д., Которые могут быть унаследованы другими слайдами. Использование мастера в PowerPoint может обеспечить согласованность стиля и избежать повторной работы. В этой статье я покажу вам, как применить два разных шаблона слайдов к разным слайдам в одном представлении PowerPoint с помощью Spire. Презентация для Java.
Образец документа
Добавьте Шпиль.Презентация.jar как зависимость
Способ 1: Скачать Шпиль. Презентация для Java пакет, распакуйте его, и вы получите Spire.Презентация.файл jar из папки “lib”. Импортируйте файл jar в свой проект в качестве зависимости.
Способ 2. Если вы создаете проект Maven, вы можете легко добавить зависимость jar, добавив следующие конфигурации в pom.xml .
com.e-iceblue e-iceblue http://repo.e-iceblue.com/nexus/content/groups/public/ e-iceblue spire.presentation 3.5.4/version>
Использование кода
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.image.BufferedImage;
import java.io.FileInputStream;
public class ApplyTwoMastersInPPT {
public static void main(String[] args) throws Exception {
//Create a Presentation object
Presentation presentation = new Presentation();
//Load the sample powerpoint document
presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pptx");
//Get the first slide master
IMasterSlide first_master = presentation.getMasters().get(0);
//Create another slide master based on the first one
presentation.getMasters().appendSlide(first_master);
IMasterSlide second_master = presentation.getMasters().get(1);
//Set different background images for the two masters
String pic1 = "C:\\Users\\Administrator\\Desktop\\image1.jpg";
String pic2 = "C:\\Users\\Administrator\\Desktop\\image2.jpg";
BufferedImage image = ImageIO.read(new FileInputStream(pic1));
IImageData imageData = presentation.getImages().append(image);
first_master.getSlideBackground().setType(BackgroundType.CUSTOM);
first_master.getSlideBackground().getFill().setFillType(FillFormatType.PICTURE);
first_master.getSlideBackground().getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
first_master.getSlideBackground().getFill().getPictureFill().getPicture().setEmbedImage(imageData);
image = ImageIO.read(new FileInputStream(pic2));
imageData = presentation.getImages().append(image);
second_master.getSlideBackground().setType(BackgroundType.CUSTOM);
second_master.getSlideBackground().getFill().setFillType(FillFormatType.PICTURE);
second_master.getSlideBackground().getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
second_master.getSlideBackground().getFill().getPictureFill().getPicture().setEmbedImage(imageData);
//Apply the first master along with the layout to the first slide
presentation.getSlides().get(0).setLayout(first_master.getLayouts().get(6));
//Apply the second master along with the layout to the rest slides
for (int i = 1; i < presentation.getSlides().getCount(); i++)
{
presentation.getSlides().get(i).setLayout(second_master.getLayouts().get(6));
}
//Save to file
presentation.saveToFile("ApplyMultiMasters.pptx", FileFormat.PPTX_2013);
presentation.dispose();
}
}
Выход
Оригинал: “https://dev.to/eiceblue/apply-two-slide-masters-in-one-presentation-in-java-48dj”