本文介绍如何使用spire.presentation for java更改powerpoint中文本的字体样式,包括字体名称,字体大小,字体颜色,粗体,斜体等。
import com.spire.presentation.*;
import com.spire.presentation.drawing.fillformattype;
import java.awt.*;
public class changefontstyles {
public static void main(string[] args) throws exception {
//创建presentation对象
presentation presentation = new presentation();
//加载示例文档
presentation.loadfromfile("c:\\users\\administrator\\desktop\\sample.pptx");
//获取文本形状
iautoshape shape = (iautoshape) presentation.getslides().get(0).getshapes().get(0);
//获取第一个段落并更改字体颜色
paragraphex paragraph = shape.gettextframe().getparagraphs().get(0);
for (int i = 0; i < paragraph.gettextranges().getcount(); i ) {
portionex textrange = paragraph.gettextranges().get(i);
textrange.getformat().getfill().setfilltype(fillformattype.solid);
textrange.getformat().getfill().getsolidcolor().setcolor(color.blue);
}
//获取第三个段落并将文字加粗,设置斜体和下划线
paragraph = shape.gettextframe().getparagraphs().get(2);
for (int i = 0; i < paragraph.gettextranges().getcount(); i ) {
portionex textrange = paragraph.gettextranges().get(i);
textrange.getformat().isbold(tristate.true);
textrange.getformat().isitalic(tristate.true);
textrange.getformat().settextunderlinetype(textunderlinetype.dashed);
}
//获取第五个段落并更改字体名称及大小
paragraph = shape.gettextframe().getparagraphs().get(4);
for (int i = 0; i < paragraph.gettextranges().getcount(); i ) {
portionex textrange = paragraph.gettextranges().get(i);
textrange.getformat().seteastasianfont(new textfont("黑体"));
textrange.getformat().setfontheight(22f);
}
//保存文档
presentation.savetofile("output/changefontstyles.pptx", fileformat.pptx_2013);
}
}