本文介绍如何使用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);
}
}