Java 創(chuàng)建并應(yīng)用PPT幻燈片母版(java制作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,可通過E-iceblue中文官網(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:UsersTest1DesktopBackground.jpg"; String logo = "C:UsersTest1Desktoplogo2.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頁幻燈片(連同默認(rèn)的幻燈片,文檔中共5頁) 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:UsersTest1DesktopImage1.jpg"; String pic2 = "C:UsersTest1DesktopImage2.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)用第一個(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)建效果:
(本文完)