Java 創(chuàng)建并應(yīng)用PPT幻燈片母版的方法示例
前言
在PowerPoint文檔中,幻燈片母版可供用戶設(shè)置幻燈片的樣式,比如標(biāo)題文字、背景、屬性等。預(yù)先設(shè)定好的幻燈片母版可用于所有幻燈片,此外,也可創(chuàng)建多個(gè)幻燈片母版分別應(yīng)用到幻燈片中。本文將介紹如何創(chuàng)建并應(yīng)用單個(gè)或多個(gè)幻燈片母版。
環(huán)境構(gòu)建
文中演示代碼用到的工具是Free Spire.Presentation for Java,可通過(guò)官網(wǎng)下載獲取。解壓后將位于lib文件夾下的Spire.Presentation.jar導(dǎo)入Java程序。此外,還可通過(guò)maven倉(cāng)庫(kù)安裝導(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)建多個(gè)母版并分別應(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頁(yè)幻燈片(連同默認(rèn)的幻燈片,文檔中共5頁(yè))
for (int i = 0; i < 4; i++)
{
presentation.getSlides().append();
}
//獲取默認(rèn)的母版
IMasterSlide first_master = presentation.getMasters().get(0);
//創(chuàng)建并獲取第二個(gè)母板
presentation.getMasters().appendSlide(first_master);
IMasterSlide second_master = presentation.getMasters().get(1);
//為兩個(gè)母版分別設(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è)應(yīng)用第一個(gè)母版及版式(板式6為空板式)
presentation.getSlides().get(0).setLayout(first_master.getLayouts().get(6));
//在剩下的幻燈片應(yīng)用第二個(gè)母版及版式
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java如何為 PPT 中的圖形添加陰影效果
- Java 在PPT中添加文本和圖片超鏈接的實(shí)現(xiàn)方法
- Java 在PPT中添加混合圖表過(guò)程詳解
- java實(shí)現(xiàn)在線預(yù)覽--poi實(shí)現(xiàn)word、excel、ppt轉(zhuǎn)html的方法
- Java 添加文本框到PPT幻燈片過(guò)程解析
- Java如何在PPT中繪制圖形
- 淺談Java設(shè)置PPT幻燈片背景——純色、漸變、圖片背景
- Java使用jacob將微軟office中word、excel、ppt轉(zhuǎn)成pdf
- Java 給PPT添加動(dòng)畫效果的示例
相關(guān)文章
SpringBoot項(xiàng)目中訪問(wèn)HTML頁(yè)面的三種方法
這篇文章主要介紹了SpringBoot項(xiàng)目中訪問(wèn)HTML頁(yè)面的三種方法,文中通過(guò)代碼示例和圖文結(jié)合的方式講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-07-07
SpringBoot+Echarts實(shí)現(xiàn)請(qǐng)求后臺(tái)數(shù)據(jù)顯示餅狀圖
這篇文章主要介紹了SpringBoot+Echarts實(shí)現(xiàn)請(qǐng)求后臺(tái)數(shù)據(jù)顯示餅狀圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
MybatisPlus多條件?or()的使用問(wèn)題小結(jié)
這篇文章主要介紹了MybatisPlus多條件?or()的使用問(wèn)題小結(jié),本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-05-05
把Java程序轉(zhuǎn)換成exe,可直接運(yùn)行的實(shí)現(xiàn)
這篇文章主要介紹了把Java程序轉(zhuǎn)換成exe,可直接運(yùn)行的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
使用自定義注解進(jìn)行restful請(qǐng)求參數(shù)的校驗(yàn)方式
這篇文章主要介紹了使用自定義注解進(jìn)行restful請(qǐng)求參數(shù)的校驗(yàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10

