本文将详细介绍如何使用spire.presentation for java给ppt文档添加文本水印。
import com.spire.presentation.*;
import com.spire.presentation.drawing.fillformattype;
import java.awt.*;
import java.awt.geom.rectangle2d;
public class watermark {
public static void main(string[] args) throws exception {
//创建presentation对象并加载示例文档
presentation presentation = new presentation();
presentation.loadfromfile("c:\\users\\administrator\\desktop\\sample.pptx");
//设置文本水印的宽和高
int width= 400;
int height= 300;
//定义一个长方形区域
rectangle2d.double rect = new rectangle2d.double((presentation.getslidesize().getsize().getwidth() - width) / 2,
(presentation.getslidesize().getsize().getheight() - height) / 2, width, height);
//添加一个shape到定义区域
iautoshape shape = presentation.getslides().get(0).getshapes().appendshape(shapetype.rectangle, rect);
//设置shape样式
shape.getfill().setfilltype(fillformattype.none);
shape.getshapestyle().getlinecolor().setcolor(color.white);
shape.setrotation(-45);
shape.getlocking().setselectionprotection(true);
shape.getline().setfilltype(fillformattype.none);
//添加文本到shape
shape.gettextframe().settext("内部使用");
portionex textrange = shape.gettextframe().gettextrange();
//设置文本水印样式
textrange.getfill().setfilltype(fillformattype.solid);
textrange.getfill().getsolidcolor().setcolor(color.pink);
textrange.setfontheight(50);
//保存文档
presentation.savetofile("output/result.pptx", fileformat.pptx_2010);
}
}