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