`

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

阅读更多

【前言】

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

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

 

(本文完)

0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics