スライドマスターによって、フォント、プレースホルダーのサイズや位置、背景のデザインや配色など、デザインテンプレート情報をスライドに事前に保存できます。セットマスターは、すべてのスライドに適用することも、複数の異なるマスターをに適用して設計することもできます。別のスライド。以下では、Javaコードの例を使用して、単一のマスターとさまざまなマスターを作成する方法について説明します。
使用したツール:Free Spire.Office for Java(無料版)
jarの取得とインポート:公式Webサイトからjarパッケージを**ダウンロード**し、libフォルダー内のjarファイルを解凍してJavaプログラムにインポートします。
##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 {
        //PowerPointドキュメントを作成し、スライドサイズを設定する
        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);
        //スライドを追加する(PowerPointドキュメントを作成する場合、デフォルトでスライドが生成されています。ここにスライドを追加して、マスターを追加した場合の結果を比較してください)
        ppt.getSlides().append();
        //ドキュメントを保存する
        ppt.saveToFile("CreateSlideMaster.pptx", FileFormat.PPTX_2013);
        ppt.dispose();
    }
}
###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{
        //PowerPointドキュメントを作成し、スライドサイズを設定する
        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);
        //2番目のマスターを作成して入手する
        ppt.getMasters().appendSlide(first_master);
        IMasterSlide second_master = ppt.getMasters().get(1);
        //2つのマスターごとに異なる背景画像を設定する
        BufferedImage image = ImageIO.read(new FileInputStream("C:\\Users\\Administrator\\Pictures\\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,400,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("C:\\Users\\Administrator\\Pictures\\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);
        //最初のマスターとレイアウトを最初のページに適用する
        ppt.getSlides().get(0).setLayout(first_master.getLayouts().get(6));
        //2番目のマスターとレイアウトを残りのスライドに適用する
        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();
    }
}
今回のPowerPointスライドマスターを作成して適用する方法は以上でした、最後まで読んでいただきありがとうございます。



