Java 在PPT中添加文本水印的简易方法(单一/平铺水印)

2023-05-24,,

【前言】

在PPT幻灯片中,可通过添加形状的方式,来实现类似水印的效果,可添加单一文本水印效果,即在幻灯片中心位置水印以单个文本字样显示,但通过一定方法也可以添加多行(平铺)文本水印效果,即在幻灯片中以一定方式平铺排列多个文本水印效果到页面上。上篇文章中介绍了通过C# 程序来添加多行水印效果,本文以Java程序代码为例介绍如何实现水印添加,包括添加单一文本水印和平铺文本内水印,代码供参考。

【程序环境】

本次程序编译环境为IntelliJ IDEA,JDK版本1.8.0,并引入free spire.presentation.jar3.9.0版本文件。

1. 添加单一文本水印

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType; import java.awt.*;
import java.awt.geom.Rectangle2D; public class TextWatermark {
public static void main(String[] args) throws Exception {
//加载示例文档
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();
}
}

单一水印效果:

2. 添加多行(平铺)文本水印

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
import java.awt.geom.Rectangle2D; public class TextWatermark2 {
public static void main(String[] args) throws Exception{
//加载PPT源文档
Presentation ppt = new Presentation();
ppt.loadFromFile("sample.pptx"); //获取指定幻灯片
ISlide slide = ppt.getSlides().get(0); //设置文本水印文本宽和高
int width= 110;
int height= 80; //起始坐标
float x = 10;
float y = 40;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
//绘制文本,设置文本格式并将其添加到第一张幻灯片
Rectangle2D.Double rect = new Rectangle2D.Double(x,y,width, height);
IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, rect);
shape.getFill().setFillType(FillFormatType.NONE);
shape.getShapeStyle().getLineColor().setColor(Color.white);
shape.setRotation(-45);
shape.getLocking().setSelectionProtection(true);
shape.getLine().setFillType(FillFormatType.NONE);
shape.getTextFrame().setText("内部使用");
shape.setShapeArrange(ShapeAlignmentEnum.ShapeArrange.SendToBack);
PortionEx textRange = shape.getTextFrame().getTextRange();
textRange.getFill().setFillType(FillFormatType.SOLID);
textRange.getFill().getSolidColor().setColor(new Color(238,130,238));
textRange.setFontHeight(20);
x += (100 + ppt.getSlideSize().getSize().getWidth()/6);
}
x = 30;
y += (100 + ppt.getSlideSize().getSize().getHeight()/7) ;
} //保存文档
ppt.saveToFile("TextWatermark2.pptx", FileFormat.PPTX_2013);
ppt.dispose();
}
}

这里通过设置文本宽度值int width的值可实现不同的水印文本字符排列样式,如下width = 110时,水印字样效果:

width=60时,水印效果:

width=10时,水印效果:

【最后】

以上是本次介绍Java添加PPT文本水印的全部内容,添加单一水印和平铺水印效果的基本方法相差不大。

(本文完,如需转载,请务必注明出处!!!)

Java 在PPT中添加文本水印的简易方法(单一/平铺水印)的相关教程结束。

《Java 在PPT中添加文本水印的简易方法(单一/平铺水印).doc》

下载本文的Word格式文档,以方便收藏与打印。