继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Java 在Word中设置多行(平铺)文本水印

Eiceblue
关注TA
已关注
手记 107
粉丝 9
获赞 48

在Word设置水印时,通常设置文本字样的水印只能在页面中设置一个水印字样,若需要在Word中设置平铺水印效果,可以参考以下方法。

使用工具:Free Spire.Doc for Java(免费版)

Jar下载及导入:

1. 手动下载导入,解压下载后的jar包,将lib文件夹下的Spire.Doc.jar文件导入java程序;

2. 创建Maven程序时,在pom.xml中配置maven仓库路径并指定Free Spire.Doc for Java的依赖,如下:

<repositories>    
    <repository>            
      <id>com.e-iceblue</id>            
      <url>http://repo.e-iceblue.cn/repository/maven-public/</url>        
    </repository>    
 </repositories>
 
 <dependencies>  
   <dependency>      
     <groupId> e-iceblue </groupId>    
         <artifactId>spire.doc.free</artifactId>      
         <version>2.7.3</version>    
   </dependency>
  </dependencies>

Jar导入结果:

http://img.mukewang.com/5f90ef4a00013e2103560338.jpg


Java 代码

 import com.spire.doc.*;
 import com.spire.doc.documents.Paragraph;
 import com.spire.doc.documents.ShapeLineStyle;
 import com.spire.doc.documents.ShapeType;
 import com.spire.doc.fields.ShapeObject;
 
 import java.awt.*;
 
 public class TextWatermark {
     public static void main(String[] args) {
         //加载示例文档
         Document doc = new Document();
         doc.loadFromFile("sample.docx");
 
         //添加艺术字并设置大小
         ShapeObject shape = new ShapeObject(doc, ShapeType.Text_Plain_Text);
         shape.setWidth(60);
         shape.setHeight(20);
         //设置艺术字文本内容、位置及样式(即文本水印字样)
         shape.setVerticalPosition(30);
         shape.setHorizontalPosition(20);
         shape.setRotation(315);
         shape.getWordArt().setText("内部使用");
         shape.setFillColor(Color.red);
         shape.setLineStyle(ShapeLineStyle.Single);
         shape.setStrokeColor(new Color(192, 192, 192, 255));
         shape.setStrokeWeight(1);
 
         Section section;
         HeaderFooter header;
         for (int n = 0; n < doc.getSections().getCount(); n++) {
             section = doc.getSections().get(n);
             //获取页眉
             header = section.getHeadersFooters().getHeader();
             Paragraph paragraph1;
             for (int i = 0; i < 4; i++) {
                 //添加4个段落到页眉
                 paragraph1 = header.addParagraph();
                 for (int j = 0; j < 3; j++) {
                     //复制艺术字并设置多行多列位置
                     shape = (ShapeObject) shape.deepClone();
                     shape.setVerticalPosition(50 + 200 * i);
                     shape.setHorizontalPosition(20 + 160 * j);
                     paragraph1.getChildObjects().add(shape);
                 }
             }
         }
 
         //保存文档
         doc.saveToFile("result.docx", FileFormat.Docx_2013);
         doc.dispose();
     }
 }

水印添加效果:

http://img2.mukewang.com/5f90ef700001dac305540430.jpg


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP