本文介绍如何使用Spire.Prensetaion for Java为PowerPoint中的文本设置透明色。
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class ApplyTransparency {
public static void main(String[] args) throws Exception {
//创建Prensetaion对象
Presentation presentation = new Presentation();
presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);
//添加一个图形
IAutoShape textbox = presentation .getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE,new Rectangle2D.Float(50, 70, 300, 120));
textbox.getShapeStyle().getLineColor().setColor(new Color(1,1,1,0));
textbox.getFill().setFillType(FillFormatType.NONE);
//删除默认段落
textbox.getTextFrame().getParagraphs().clear();
//添加三个段落,并对段落文字设置不同透明度的颜色
int alpha = 55;
for (int i = 0; i < 3; i++)
{
textbox.getTextFrame().getParagraphs().append(new ParagraphEx());
textbox.getTextFrame().getParagraphs().get(i).getTextRanges().append(new PortionEx("Text Transparency"));
textbox.getTextFrame().getParagraphs().get(i).getTextRanges().get(0).getFill().setFillType(FillFormatType.NONE);
textbox.getTextFrame().getParagraphs().get(i).getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
textbox.getTextFrame().getParagraphs().get(i).getTextRanges().get(0).getFill().getSolidColor().setColor(new Color(176, 48, 96, alpha));
alpha += 100;
}
//保存文档
presentation.saveToFile("TextTransparency.pptx", FileFormat.PPTX_2013);
}
}