如何使用 Java 为 PowerPoint 演示文稿添加文本水印
Source: Dev.to
Spire.Presentation for Java:简介与安装
Spire.Presentation for Java 是一个功能强大、特性丰富的 API,能够让 Java 应用程序创建、读取、写入和转换 PowerPoint 演示文稿。它提供了对演示文稿元素的广泛管理能力,包括幻灯片、形状、文本、图像以及添加水印的功能。该库简化了复杂的 PowerPoint 自动化任务,是开发者的宝贵工具。
要将 Spire.Presentation 集成到项目中,最简便的方法是添加其 Maven 依赖。这可以确保所有必需的库自动管理并包含在构建中。
Maven 依赖
<!-- Repository definition -->
<repository>
<id>e-iceblue</id>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
<!-- Dependency -->
<dependency>
<groupId>com.e-iceblue</groupId>
<artifactId>spire.presentation</artifactId>
<version>10.11.4</version>
</dependency>
如果不使用 Maven,也可以直接从 E‑iceblue 官网下载 JAR 文件并手动添加到项目的构建路径中。不过,通常建议使用 Maven 以便更轻松地管理依赖。
使用 Java 为 PowerPoint 添加单行文本水印
单行文本水印非常适合用于细腻但清晰的品牌标识或内容状态指示,如 “CONFIDENTIAL”, “DRAFT”, “INTERNAL USE ONLY”。 这种方式通常将水印斜放在幻灯片上,确保可见性而不遮挡主要内容。
步骤
- 创建并加载 PowerPoint 文件 – 实例化
Presentation对象并调用Presentation.loadFromFile()。 - 定义水印尺寸 – 设置文本框的宽度和高度。
- 创建矩形形状 – 使用
Rectangle2D.Double根据幻灯片尺寸定位形状。 - 将形状添加到幻灯片 – 使用
Presentation.getSlides().get(0).getShapes().appendShape()将矩形追加到第一张幻灯片。 - 自定义形状样式 – 调整边框、填充透明度、旋转角度和对齐方式。
- 添加并格式化水印文本 – 通过
IAutoShape.getTextFrame().setText()设置文本,然后对TextRange(字体、大小、颜色、透明度)进行样式设置。 - 保存演示文稿 – 使用
Presentation.saveToFile()导出更新后的文件。
代码示例
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class InsertSingleWatermark {
public static void main(String[] args) throws Exception {
// Create a Presentation object and load a sample file
Presentation presentation = new Presentation();
presentation.loadFromFile("sample.pptx");
// Set the width and height of watermark string
int width = 400;
int height = 300;
// Define the position and size of shape
Rectangle2D.Double rect = new Rectangle2D.Double(
(presentation.getSlideSize().getSize().getWidth() - width) / 2,
(presentation.getSlideSize().getSize().getHeight() - height) / 2,
width, height);
// Add the shape to the first slide
IAutoShape shape = presentation.getSlides().get(0).getShapes()
.appendShape(ShapeType.RECTANGLE, rect);
// Set the style of shape
shape.getFill().setFillType(FillFormatType.NONE);
shape.getShapeStyle().getLineColor().setColor(Color.white);
shape.setRotation(-45);
shape.getLocking().setSelectionProtection(true);
shape.getLine().setFillType(FillFormatType.NONE);
// Add text to shape
shape.getTextFrame().setText("Confidential");
PortionEx textRange = shape.getTextFrame().getTextRange();
// Set the style of the text range
textRange.getFill().setFillType(FillFormatType.SOLID);
textRange.setFontHeight(50);
Color color = new Color(237, 129, 150, 200); // RGBA with transparency
textRange.getFill().getSolidColor().setColor(color);
// Save the result document
presentation.saveToFile("output/SingleWatermark.pptx", FileFormat.PPTX_2010);
presentation.dispose();
}
}
该代码片段演示了加载演示文稿、创建文本框、设置文本及其格式(包括颜色、字体大小和透明度)、旋转文本框,最后保存演示文稿的完整过程。透明度和旋转是实现合适水印效果的关键。
使用 Java 为 PowerPoint 添加平铺文本水印(多行)
对于需要更强内容保护或全局品牌展示的场景,平铺水印可以在整张幻灯片上重复出现文字。这使得水印更难被裁剪或忽视,特别适用于高度敏感的文档。
步骤
- 加载 PowerPoint 演示文稿 – 创建
Presentation对象并调用loadFromFile()。 - 准备水印文本并测量其尺寸 – 定义要重复的字符串并计算其宽高。
- 添加重复的水印形状 – 初始化起始坐标,在幻灯片布局中循环,使用
appendShape()插入矩形形状,以分布多个水印。 - 格式化矩形形状 – 应用填充透明度、去除边框、设置旋转等,使水印保持低调。
- 为每个形状应用文本 – 通过
IAutoShape.getTextFrame().setText()设置文本,并获取TextRange进行进一步样式设置。 - 格式化水印文本 – 自定义字体大小、颜色、不透明度、间距和旋转角度。
- 保存文件 – 使用
Presentation.saveToFile()导出最终版本。
代码示例
import com.spire.pdf.graphics.PdfTrueTypeFont;
import com.spire.presentat
// (The remainder of the example code should follow the same pattern as the single‑line watermark,
// creating multiple shapes in a loop, setting their text, styling, and then saving the file.)
注意:上述代码片段不完整;开发者应通过遍历幻灯片区域、创建形状、应用所需文本和样式,然后参考单行示例的保存方式来完成实现。