Java 创建并应用PPT幻灯片母版(java制作ppt)

前言

在PowerPoint文档中,幻灯片母版可供用户设置幻灯片的样式,比如标题文字、背景、属性等。预先设定好的幻灯片母版可用于所有幻灯片,此外,也可创建多个幻灯片母版分别应用到幻灯片中。本文将介绍如何创建并应用单个或多个幻灯片母版。

环境构建

文中代码演示用到的工具是Free Spire.Presentation for Java,可通过E-iceblue中文官网下载获取。解压后将位于lib文件夹下的Spire.Presentation.jar导入Java程序(如下图所示)。此外,还可通过maven仓库安装导入。

Java 创建并应用PPT幻灯片母版(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 UniqueSlideMaster { public static void main(String[] args) throws Exception { //创建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"; //设置母版背景 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(); }}

创建效果:

Java 创建并应用PPT幻灯片母版(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.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); //创建并获取第二个母板 presentation.getMasters().appendSlide(first_master); IMasterSlide second_master = presentation.getMasters().get(1); //为两个母版分别设置不同的背景图片 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); //在第一页应用第一个母版及版式(板式6为空板式) presentation.getSlides().get(0).setLayout(first_master.getLayouts().get(6)); //在剩下的幻灯片应用第二个母版及版式 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(); }}

创建效果:

Java 创建并应用PPT幻灯片母版(java制作ppt)

(本文完)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

(0)
上一篇 2024年5月21日 上午9:17
下一篇 2024年5月21日 上午9:29

相关推荐

  • 落实中央财政科研项目

    落实中央财政科研项目 近年来,中国政府一直致力于推动科技创新,加强国家实力。为此,政府出台了一系列政策,支持中央财政科研项目的落实。本文将探讨如何落实中央财政科研项目,以及其对科技…

    科研百科 2025年3月28日
    0
  • 科研项目评审主持

    科研项目评审主持的工作是非常重要的,它直接关系到科研项目的质量和进度。作为科研项目评审主持,我们需要认真对待每一个评审环节,确保评审过程的公正性和客观性。 在评审过程中,我们需要遵…

    科研百科 2025年2月21日
    0
  • java学生管理系统项目名称

    Java学生管理系统项目概述 Java学生管理系统是一款基于Java语言的面向对象编程软件,主要用于管理学生信息、课程信息、成绩信息等。该系统可以实现学生信息的增删改查、课程信息的…

    科研百科 2025年1月14日
    0
  • 惠城区协同办公自动化系统

    惠城区协同办公自动化系统 随着现代办公需求的不断增长,协同办公自动化系统成为了企业必备的办公软件之一。惠城区协同办公自动化系统是一种能够协助企业进行多人协同工作、高效管理文件和流程…

    科研百科 2024年8月29日
    27
  • 到底什么是项目质量管理体系?(到底什么是项目质量管理体系建设)

    在做项目管理时,建立并不断完善质量管理体系,是整个质量管理的核心内容,它将为项目质量保证奠定坚实的基础。 一般来说,项目质量管理体系主要是由以下几个质量保证体系构成的—— (1)质…

    2022年8月25日
    413
  • 党建基础设施问题

    党建基础设施问题 在中国共产党的领导下,我国各个领域的发展水平不断提高,但是党建基础设施问题却一直得不到有效的解决。这个问题不仅关系到党的执政能力和执政地位,也关系到党员和干部的健…

    科研百科 2025年1月3日
    0
  • 教你用富士X100V相机定制属于自己的独特色彩(富士x100v摄影作品)

    在摄影里谈色彩,大部分用户想到的都是后期,但是我们今天谈的却是前期拍摄。富士旗下的很多相机都具有内置的色彩管理选项,比如近期非常火热的X100V。富士X100V除了内置丰富的胶片模…

    科研百科 2023年5月20日
    244
  • 陪诊小程序开发多少钱(陪诊怎么找客户)

    开发一个基本的陪诊小程序需要至少2名开发人员,涉及到前端及后端的工作,开发周期大约在1个月左右。根据不同地区劳动力成本而言,开发一个基础小程序的价格通常在2万-3万人民币左右。 如…

    科研百科 2023年5月6日
    283
  • 工程项目管理是一个系统

    工程项目管理是一个系统 工程项目管理是一个系统,能够帮助项目经理和项目团队成员更好地协调和管理项目。这个系统涵盖了许多方面,包括项目计划、进度跟踪、成本估算、风险管理、质量管理和团…

    科研百科 2025年1月27日
    0
  • 小程序游戏的外包开发流程(小程序游戏的外包开发流程是什么)

    小程序游戏是指在微信小程序平台上开发的游戏应用程序,非常适合规模比较小的游戏,即玩即走,碎片时间拿起来就玩,快速打开快速运行,是游戏开发不可缺少的运行模式。另外,依托于微信平台社交…

    科研百科 2023年5月2日
    218