java 生成pptx_Java 创建并应用PPT幻灯片母版

该博客介绍了如何使用Java通过Spire.Presentation库创建PPTX文档,并设置不同幻灯片母版。代码示例展示了如何插入幻灯片,设置母版背景图片,以及应用母版到特定幻灯片。
摘要由CSDN通过智能技术生成

import com.spire.presentation.*;importcom.spire.presentation.drawing.BackgroundType;importcom.spire.presentation.drawing.FillFormatType;importcom.spire.presentation.drawing.IImageData;importcom.spire.presentation.drawing.PictureFillType;importjavax.imageio.ImageIO;import java.awt.*;importjava.awt.geom.Rectangle2D;importjava.awt.image.BufferedImage;importjava.io.FileInputStream;public classCreateMasterSlide2 {public static void main(String[] args) throwsException{//创建PPT文档,并设置幻灯片大小

Presentation ppt = newPresentation();

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);//创建并获取第二个母板

ppt.getMasters().appendSlide(first_master);

IMasterSlide second_master= ppt.getMasters().get(1);//为两个母版分别设置不同的背景图片

BufferedImage image = ImageIO.read(new FileInputStream("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,200,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("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);//在第一页应用第一个母版及版式(板式6为空板式)

ppt.getSlides().get(0).setLayout(first_master.getLayouts().get(6));//在剩下的幻灯片应用第二个母版及版式

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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值