Java在PPT幻灯片中添加文字水印效果 如何利用Java在图片上添加文字水印效果。在PPT中没有直接添加水印的功能,要实现水印效果,可以通过以下思路来实现水印效果:添加形状,在形状中添加文本,设置形状置于底层(防止文本遮盖幻灯片内容),下面通过Java程序代码示例来介绍如何实现。
工具/材料
IntelliJIDEA
Jdk版本1.8.0
Spire.Presentation.jar版本3.9.0
操作方法
01
准备一个需要添加水印的PPT幻灯片文档,如图:
02
在IDEA程序中导入jar文件后,键入如下代码内容:
importcom.spire.presentation.*;
importcom.spire.presentation.drawing.FillFormatType;
importjava.awt.*;
importjava.awt.geom.Rectangle2D;
publicclassTextWatermark{
publicstaticvoidmain(String[]args)throwsException{
//创建presentation对象并加载示例文档
Presentationppt=newPresentation();
ppt.loadFromFile("sample.pptx");
//获取指定幻灯片
ISlideslide=ppt.getSlides().get(0);
//设置文本水印的宽和高
intwidth=400;
intheight=300;
//定义一个长方形区域
Rectangle2D.Doublerect=newRectangle2D.Double((ppt.getSlideSize().getSize().getWidth()-width)/2,
(ppt.getSlideSize().getSize().getHeight()-height)/2,width,height);
//添加一个shape到定义区域
IAutoShapeshape=slide.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.setShapeArrange(ShapeAlignmentEnum.ShapeArrange.SendToBack);
//添加文本到shape
shape.getTextFrame().setText("内部使用");
PortionExtextRange=shape.getTextFrame().getTextRange();
//设置文本水印样式
textRange.getFill().setFillType(FillFormatType.SOLID);
textRange.getFill().getSolidColor().setColor(newColor(211,211,211));
textRange.setFontHeight(50);
//保存文档
ppt.saveToFile("TextWatermark.pptx",FileFormat.PPTX_2013);
ppt.dispose();
}
}
03
完成代码后,运行程序,生成文档。在IDEA项目文件夹下,查看结果文档,如下水印添加效果:
温馨提示:答案为网友推荐,仅供参考