java幻灯片播放代码_Java如何在PPT中的幻灯片上格式化文本?

在Java编程中,如何在PPT中的幻灯片上格式化文本?

注意:需要访问网址:http://poi.apache.org/download.html , 下载一个Apache POI软件包。这里下载最新版本:poi-bin-3.17-20170915.tar.gz解压并全部导入 。

需要导入全部包,如下图所示 -

1d585dd05552a89f3d62bc16b055504d.png

以下是使用Java在PPT中的幻灯片上格式化文本的程序。

package com.yiibai;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import org.apache.poi.xslf.usermodel.SlideLayout;

import org.apache.poi.xslf.usermodel.XMLSlideShow;

import org.apache.poi.xslf.usermodel.XSLFSlide;

import org.apache.poi.xslf.usermodel.XSLFSlideLayout;

import org.apache.poi.xslf.usermodel.XSLFSlideMaster;

import org.apache.poi.xslf.usermodel.XSLFTextParagraph;

import org.apache.poi.xslf.usermodel.XSLFTextRun;

import org.apache.poi.xslf.usermodel.XSLFTextShape;

public class FormatTextPPT {

public static void main(String args[]) throws IOException {

// creating an empty presentation

XMLSlideShow ppt = new XMLSlideShow();

// getting the slide master object

// XSLFSlideMaster slideMaster = ppt.getSlideMasters()[0];

java.util.List sl = ppt.getSlideMasters();

XSLFSlideMaster slideMaster = sl.get(0);

// select a layout from specified list

XSLFSlideLayout slidelayout = slideMaster.getLayout(SlideLayout.TITLE_AND_CONTENT);

// creating a slide with title and content layout

XSLFSlide slide = ppt.createSlide(slidelayout);

// selection of title place holder

XSLFTextShape body = slide.getPlaceholder(1);

// clear the existing text in the slide

body.clearText();

// adding new paragraph

XSLFTextParagraph paragraph = body.addNewTextParagraph();

// formatting line 1

XSLFTextRun run1 = paragraph.addNewTextRun();

run1.setText(" 这是红色的一行文本字符串 ");

// setting color to the text

run1.setFontColor(java.awt.Color.red);

// setting font size to the text

run1.setFontSize(24.00);

// moving to the next line

paragraph.addLineBreak();

// formatting line 2

XSLFTextRun run2 = paragraph.addNewTextRun();

run2.setText(" 这是一行加粗的字符串 ");

run2.setFontColor(java.awt.Color.CYAN);

// making the text bold

run2.setBold(true);

paragraph.addLineBreak();

// formatting line 3

XSLFTextRun run3 = paragraph.addNewTextRun();

run3.setText(" 这是加了划线的字符串");

run3.setFontSize(12.00);

// making the text italic

run3.setItalic(true);

// strike through the text

run3.setStrikethrough(true);

paragraph.addLineBreak();

// formatting line 4

XSLFTextRun run4 = paragraph.addNewTextRun();

run4.setText(" 这是加了下划线的文本 ");

run4.setUnderlined(true);

// underlining the text

paragraph.addLineBreak();

// creating a file object

File file = new File("FormatedText.pptx");

FileOutputStream out = new FileOutputStream(file);

// saving the changes to a file

ppt.write(out);

out.close();

System.out.println("PPT created");

}

}

执行上面示例代码,得到以下结果 -

PPT created

这就创建一个PPT文件:FormatedText.pptx了,如下所示 -

b5ec26b7263427a52f3bc06ac81c419e.png

¥ 我要打赏

纠错/补充

收藏

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值