欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实现给Word文件添加文字水印

程序员文章站 2022-06-03 23:05:45
目录方法思路jar引入java代码word中设置水印时,可预设的文字或自定义文字设置为水印效果,但通常添加水印效果时,会对所有页面都设置成统一效果,如果需要对每一页或者某个页面设置不同的水印效果,则可...

word中设置水印时,可预设的文字或自定义文字设置为水印效果,但通常添加水印效果时,会对所有页面都设置成统一效果,如果需要对每一页或者某个页面设置不同的水印效果,则可以参考本文中的方法。下面,将以java代码为例,对word每一页设置不同的文字水印效果作详细介绍。

方法思路

在给word每一页添加水印前,首先需要在word文档每一页正文的最后一个字符后面插入“连续”分节符,然后在每一节的页眉段落里添加艺术字类型的形状对象,并设置艺术字的坐标位置、样式、对齐方式等。最后保存文档。

jar引入

在程序中引入 free spire.doc for java 中的spire.doc.jar文件(该文件在lib文件夹下);如果需要通过maven下载导入,可进行如下配置pom.xml:

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
        </repository>
    </repositories>
<dependencies>
    <dependency>
        <groupid>e-iceblue</groupid>
        <artifactid>spire.doc.free</artifactid>
        <version>5.1.0</version>
    </dependency>
</dependencies>

java代码

给每页添加图片水印时,可参考如下步骤:

  1. 创建document类的对象,并通过document.loadfromfile(string filename)方法加载word文档。
  2. 通过document.getsections().get(int index)方法获取指定节。
  3. 通过section.getheadersfooters().getheader()方法获取页眉,headerfooter.addparagraph()方法添加段落到页眉。
  4. 创建shapeobject类的对象,并传入参数设置形状类型为text_plain_text类型的艺术字。并调用方法设置艺术字样式,如艺术字高度、宽度、旋转、颜色、对齐方式等。
  5. 通过paragraph.getchildobjects().add(idocumentobject entity)方法添加艺术字到段落。
  6. 最后,通过document.savetofile(string filename, fileformat fileformat)方法保存文档。

不同页面中设置不一样的文字水印效果,只需要获取该页面对应节的页眉段落,然后参考上述用到的方法步骤逐一添加即可。

下面是完整的java代码示例:

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.shapeobject;

import java.awt.*;

public class differenttextwatermark {
    public static void main(string[] args) {
        //加载word测试文档
        document doc = new document();
        doc.loadfromfile("test.docx");

        //获取文档第一节
        section section1 = doc.getsections().get(0);

        //定义水印文字的纵向坐标位置
        float y = (float) (section1.getpagesetup().getpagesize().getheight()/3);

        //添加文字水印1
        headerfooter header1 = section1.getheadersfooters().getheader();//获取页眉
        header1.getparagraphs().clear();//删除原有页眉格式的段落
        paragraph para1= header1.addparagraph();//重新添加段落
        //添加艺术字并设置大小
        shapeobject shape1 = new shapeobject(doc, shapetype.text_plain_text);
        shape1.setwidth(362);
        shape1.setheight(118);
        //设置艺术字文本内容、位置及样式(即文本水印字样)
        shape1.setrotation(315);
        shape1.getwordart().settext("内部使用");
        shape1.setfillcolor(new color(128,128,128));
        shape1.setlinestyle(shapelinestyle.single);
        shape1.setstrokecolor(new color(128,128,128));
        shape1.setstrokeweight(0.5);
        shape1.setverticalposition(y);
        shape1.sethorizontalalignment(shapehorizontalalignment.center);
        para1.getchildobjects().add(shape1);

        //同理设置第二节页眉中的文字水印2
        section section2 = doc.getsections().get(1);
        headerfooter header2 = section2.getheadersfooters().getheader();
        header2.getparagraphs().clear();
        paragraph para2= header2.addparagraph();
        shapeobject shape2 = new shapeobject(doc, shapetype.text_plain_text);
        shape2.setwidth(362);
        shape2.setheight(118);
        shape2.setrotation(315);
        shape2.getwordart().settext("绝密资料");
        shape2.setfillcolor(new color(221,160,221));
        shape2.setlinestyle(shapelinestyle.single);
        shape2.setstrokecolor(new color(221,160,221));
        shape2.setstrokeweight(0.5);
        shape2.setverticalposition(y);
        shape2.sethorizontalalignment(shapehorizontalalignment.center);
        para2.getchildobjects().add(shape2);

        //同理设置第三节中的页眉中的文字水印3
        section section3 = doc.getsections().get(2);
        headerfooter header3 = section3.getheadersfooters().getheader();
        header3.getparagraphs().clear();
        paragraph para3= header3.addparagraph();
        shapeobject shape3 = new shapeobject(doc, shapetype.text_plain_text);
        shape3.setwidth(362);
        shape3.setheight(118);
        shape3.setrotation(315);
        shape3.getwordart().settext("禁止传阅");
        shape3.setfillcolor(new color(70,130,180));
        shape3.setlinestyle(shapelinestyle.single);
        shape3.setstrokecolor(new color(70,130,180));
        shape3.setstrokeweight(0.5);
        shape3.setverticalposition(y);
        shape3.sethorizontalalignment(shapehorizontalalignment.center);
        para3.getchildobjects().add(shape3);

        //保存文档
        doc.savetofile("differenttextwatermark.docx",fileformat.docx_2013);
        doc.dispose();
    }
}

如图,每一页均可显示不同的文字水印效果:

Java实现给Word文件添加文字水印

到此这篇关于java实现给word文件添加文字水印的文章就介绍到这了,更多相关java word文字水印内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!