Java 創建並應用PPT幻燈片母版

幻燈片母版,可在幻燈片中預先存儲設計模板信息,包括字形、佔位符大小或位置、背景設計和配色方案等;對設定好的母版可應用於所有幻燈片,也可設計多個不同母版應用於不同幻燈片。下面通過Java代碼示例介紹如何創建單一母版以及不同母版。
使用工具:Free Spire.Office for Java(免費版)
Jar獲取及導入:官網下載jar包,並解壓將lib文件夾下的jar文件導入Java程序,或者通過maven倉庫下載導入
如下導入效果:
Java 創建並應用PPT幻燈片母版

Java 代碼示例

1. 創建單一母版,應用到所有幻燈片

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.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;

public class CreateMasterSlide {
    public static void main(String[] args) throws Exception {
        //創建PPT文檔,並設置幻燈片大小
        Presentation ppt = new Presentation();
        ppt.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

        //獲取第一張母版
        IMasterSlide masterSlide = ppt.getMasters().get(0);

        //設置母版背景
        BufferedImage image = ImageIO.read(new FileInputStream("tp.png"));
        IImageData imageData = ppt.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);

        //添加圖片到母版
        image = ImageIO.read(new FileInputStream("logo.png"));
        imageData = ppt.getImages().append(image);
        IEmbedImage imageShape = masterSlide.getShapes().appendEmbedImage(ShapeType.RECTANGLE,imageData,new Rectangle2D.Float((float) ppt.getSlideSize().getSize().getWidth()-240,40,60,60));
        imageShape.getLine().setFillType(FillFormatType.NONE);

        //添加文字到母版
        IAutoShape textShape = masterSlide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Float((float) ppt.getSlideSize().getSize().getWidth()-230,85,200,30));
        textShape.getTextFrame().setText("文娛傳媒");
        textShape.getTextFrame().getTextRange().setFontHeight(20f);
        textShape.getTextFrame().getTextRange().getFill().setFillType(FillFormatType.SOLID);
        textShape.getTextFrame().getTextRange().getFill().getSolidColor().setColor(Color.black);
        textShape.getTextFrame().getTextRange().getParagraph().setAlignment(TextAlignmentType.CENTER);
        textShape.getFill().setFillType(FillFormatType.NONE);
        textShape.getLine().setFillType(FillFormatType.NONE);

        //添加一張幻燈片(創建PPT文檔時,已默認生成一張幻燈片,這裏添加一張幻燈片可對比查看母版添加效果)
        ppt.getSlides().append();

        //保存文檔
        ppt.saveToFile("CreateSlideMaster.pptx", FileFormat.PPTX_2013);
        ppt.dispose();
    }
}

母版創建效果:
Java 創建並應用PPT幻燈片母版

2. 創建多個母版,應用於不同幻燈片

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.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;

public class CreateMasterSlide2 {
    public static void main(String[] args) throws Exception{
        //創建PPT文檔,並設置幻燈片大小
        Presentation ppt = new Presentation();
        ppt.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

        //插入4頁幻燈片(連同默認的幻燈片,文檔中共5頁)
        for (int i = 0; i < 4; i++)
        {
            ppt.getSlides().append();
        }

        //獲取默認的母版
        IMasterSlide first_master = ppt.getMasters().get(0);

        //創建並獲取第二個母板
        ppt.getMasters().appendSlide(first_master);
        IMasterSlide second_master = ppt.getMasters().get(1);

        //爲兩個母版分別設置不同的背景圖片
        BufferedImage image = ImageIO.read(new FileInputStream("pic1.png"));
        IImageData imageData = ppt.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);
        IAutoShape textShape = first_master.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Float((float) ppt.getSlideSize().getSize().getWidth()/3,180,200,30));
        textShape.getTextFrame().setText("首頁母版");
        textShape.getTextFrame().getTextRange().setFontHeight(40f);
        textShape.getTextFrame().getTextRange().getFill().setFillType(FillFormatType.SOLID);
        textShape.getTextFrame().getTextRange().getFill().getSolidColor().setColor(Color.red);
        textShape.getTextFrame().getTextRange().getParagraph().setAlignment(TextAlignmentType.CENTER);
        textShape.getFill().setFillType(FillFormatType.NONE);
        textShape.getLine().setFillType(FillFormatType.NONE);

        image = ImageIO.read(new FileInputStream("pic2.png"));
        imageData = ppt.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);

        //在第一頁應用第一個母版及版式(板式6爲空板式)
        ppt.getSlides().get(0).setLayout(first_master.getLayouts().get(6));

        //在剩下的幻燈片應用第二個母版及版式
        for (int i = 1; i < ppt.getSlides().getCount(); i++)
        {
            ppt.getSlides().get(i).setLayout(second_master.getLayouts().get(6));
        }

        //保存文檔
        ppt.saveToFile("MultiSlideMaters.pptx", FileFormat.PPTX_2013);
        ppt.dispose();
    }
}

多個母版創建效果:
Java 創建並應用PPT幻燈片母版

(本文完)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章