本文将详细介绍如何在Java应用程序中给PowerPoint文档添加多行多列文本水印。
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class WatermarkDemo {
public static void main(String[] args) throws Exception {
Presentation presentation = new Presentation();
presentation.loadFromFile("Sample.pptx");
//设置文本水印的宽和高
int width= 300;
int height= 200;
//绘制文本,设置文本格式并将其添加到第一张幻灯片
float x = 30;
float y = 80;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Rectangle2D.Double rect = new Rectangle2D.Double(x,y,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(20);
x += (100 + presentation.getSlideSize().getSize().getWidth()/5);
}
x = 30;
y += (100 + presentation.getSlideSize().getSize().getHeight()/6);
}
//保存文档
presentation.saveToFile("output/result.pptx", FileFormat.PPTX_2010);
}
}
效果图: