欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java 創(chuàng)建并應(yīng)用PPT幻燈片母版的方法示例

 更新時間:2020年06月03日 14:49:48   作者:Tina_Tang  
幻燈片母版可供用戶設(shè)置幻燈片的樣式,本文將介紹如何用Java創(chuàng)建并應(yīng)用單個或多個幻燈片母版。感興趣可以了解一下

前言

在PowerPoint文檔中,幻燈片母版可供用戶設(shè)置幻燈片的樣式,比如標題文字、背景、屬性等。預(yù)先設(shè)定好的幻燈片母版可用于所有幻燈片,此外,也可創(chuàng)建多個幻燈片母版分別應(yīng)用到幻燈片中。本文將介紹如何創(chuàng)建并應(yīng)用單個或多個幻燈片母版。

環(huán)境構(gòu)建

文中演示代碼用到的工具是Free Spire.Presentation for Java,可通過官網(wǎng)下載獲取。解壓后將位于lib文件夾下的Spire.Presentation.jar導(dǎo)入Java程序。此外,還可通過maven倉庫安裝導(dǎo)入。

Java代碼示例

示例1 創(chuàng)建唯一母版,并應(yīng)用于所有幻燈片

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 UniqueSlideMaster {
  public static void main(String[] args) throws Exception {

    //創(chuàng)建PPT文檔,指定幻燈片大小
    Presentation presentation = new Presentation();
    presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

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

    //獲取圖片地址
    String backgroundPic = "C:\\Users\\Test1\\Desktop\\Background.jpg";
    String logo = "C:\\Users\\Test1\\Desktop\\logo2.png";

    //設(shè)置母版背景
    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);

    //添加圖片(公司Logo)到母版
    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,100));
    imageShape.getLine().setFillType(FillFormatType.NONE);

    //添加文字(公司名稱)到母版
    IAutoShape textShape = masterSlide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Float((float) presentation.getSlideSize().getSize().getWidth()-200,(float) presentation.getSlideSize().getSize().getHeight()-60,200,30));//Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(ppt.SlideSize.Size.Width-200, ppt.SlideSize.Size.Height-60, 200, 30));
    textShape.getTextFrame().setText("鷹翔傳媒有限公司");
    textShape.getTextFrame().getTextRange().setFontHeight(15f);
    textShape.getTextFrame().getTextRange().getFill().setFillType(FillFormatType.SOLID);
    textShape.getTextFrame().getTextRange().getFill().getSolidColor().setColor(Color.blue);
    textShape.getTextFrame().getTextRange().getParagraph().setAlignment(TextAlignmentType.CENTER);
    textShape.getFill().setFillType(FillFormatType.NONE);
    textShape.getLine().setFillType(FillFormatType.NONE);

    //添加一張幻燈片
    presentation.getSlides().append();

    //保存文檔
    presentation.saveToFile("output/SlideMaster.pptx", FileFormat.PPTX_2013);
    presentation.dispose();
  }
}

創(chuàng)建效果:

示例2 創(chuàng)建多個母版并分別應(yīng)用到幻燈片

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 MultiSlideMasters {
  public static void main(String[] args)throws Exception {
    //新建PPT文檔
    Presentation presentation = new Presentation();
    presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

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

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

    //創(chuàng)建并獲取第二個母板
    presentation.getMasters().appendSlide(first_master);
    IMasterSlide second_master = presentation.getMasters().get(1);

    //為兩個母版分別設(shè)置不同的背景圖片
    String pic1 = "C:\\Users\\Test1\\Desktop\\Image1.jpg";
    String pic2 = "C:\\Users\\Test1\\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);

    //在第一頁應(yīng)用第一個母版及版式(板式6為空板式)
    presentation.getSlides().get(0).setLayout(first_master.getLayouts().get(6));

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

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

創(chuàng)建效果:

到此這篇關(guān)于Java 創(chuàng)建并應(yīng)用PPT幻燈片母版的方法示例的文章就介紹到這了,更多相關(guān)Java 創(chuàng)建PPT幻燈片母版內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論