ITeye博客

Java 在PPT中添加多行文本水印

Miaonly · 2021-03-15 11:51 · Java PPT · 编程语言 · Markdown

【前言】

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

 

【程序环境】

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

 

全部编译代码如下:

Java代码 

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

 完成代码后,运行程序,在生成的结果文档中可查看水印效果。代码中的文件路径为IDEA项目文件夹路径,文件路径可自行定义。

 

(本文完)

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。