首页 > 知识问答文章正文

Java在PPT幻灯片中添加文字水印效果

知识问答 2022-02-15 05:19:23 admin

在PPT中没有直接添加水印的功能,要实现水印效果,可以通过以下思路来实现水印效果:添加形状,在形状中添加文本,设置形状置于底层(防止文本遮盖幻灯片内容),下面通过Java程序代码示例来介绍如何实现。

工具/材料

IntelliJ IDEA

Jdk版本1.8.0

Spire.Presentation.jar版本3.9.0

操作方法

【步骤01】

准备一个需要添加水印的PPT幻灯片文档,如图:

【步骤02】

在IDEA程序中导入jar文件后,键入如下代码内容:
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.*;
import java.awt.geom.Rectangle2D;

public class TextWatermark {
publicstatic void main(String[] args) throws Exception {
//创建presentation对象并加载示例文档
Presentation ppt = new Presentation();
ppt.loadFromFile("sample.pptx");

//获取指定幻灯片
ISlide slide = ppt.getSlides().get(0);

//设置文本水印的宽和高
int width= 400;
int height= 300;

//定义一个长方形区域
Rectangle2D.Double rect = new Rectangle2D.Double((ppt.getSlideSize().getSize().getWidth() - width) / 2,
(ppt.getSlideSize().getSize().getHeight() - height) / 2, width, height);

//添加一个shape到定义区域
IAutoShape shape = 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("内部使用");
PortionEx textRange = shape.getTextFrame().getTextRange();

//设置文本水印样式
textRange.getFill().setFillType(FillFormatType.SOLID);
textRange.getFill().getSolidColor().setColor(new Color(211,211,211));
textRange.setFontHeight(50);

//保存文档
ppt.saveToFile("TextWatermark.pptx", FileFormat.PPTX_2013);
ppt.dispose();
}
}

【步骤03】

完成代码后,运行程序,生成文档。在IDEA项目文件夹下,查看结果文档,如下水印添加效果:

标签:

发表评论

评论列表

可盼网Copyright @ 2011-2024 All Rights Reserved. |版权所有 备案号: 豫ICP备2022006457号-8