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

发布时间:2022-02-16 09:07:42 作者:小新
来源:亿速云 阅读:1038

这篇文章主要为大家展示了“Java如何实现给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文件添加文字水印”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. Java添加水印到Word文档
  2. Java如何实现给图片添加图片水印

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java word

上一篇:如何使用HTML+JS实现在线朗读器

下一篇:Vue3中怎么实现过渡动画效果

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》