Java 在PPT中添加多行文本水印
程序员文章站
2022-03-01 20:37:51
...
【前言】
在PPT幻灯片中,可通过添加形状的方式,来实现类似水印的效果,可添加单一文本水印效果,即在幻灯片中心位置水印以单个文本字样显示,但通过一定方法也可以添加多行(平铺)文本水印效果,即在幻灯片中以一定方式平铺排列多个文本水印效果到页面上。本文以Java程序代码为例介绍具体实现方法,代码供参考。
【程序环境】
本次程序编译环境为IntelliJ IDEA,JDK版本1.8.0,并引入free spire.presentation.jar3.9.0版本文件。
全部编译代码如下:
- 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();
- }
- }
完成代码后,运行程序,在生成的结果文档中可查看水印效果。代码中的文件路径为IDEA项目文件夹路径,文件路径可自行定义。
(本文完)