首页 > 软件开发 > JAVA >

Java 给Word每一页设置不同文字水印效果

来源:互联网 2023-03-16 19:10:53 217

Word中设置水印时,可预设的文字或自定义文字设置为水印效果,但通常添加水印效果时,会对所有页面都设置成统一效果,如果需要对每一页或者某个页面设置不同的水印效果,则可以参考下面的方法。fhd办公区 - 实用经验教程分享!

工具/原料

  • IntelliJ IDEA 2018
  • Word文档
  • Free Spire.Doc for Java

导入jar

  • 1

    将Word库下载到本地,并解压,找到lib文件夹下的jar文件。然后在IDEA程序中打开“Project Structure”,执行如图步骤:fhd办公区 - 实用经验教程分享!

    Java 给Word每一页设置不同文字水印效果fhd办公区 - 实用经验教程分享!

  • 2

    找到本地路径下的jar文件,点击“OK”,添加到列表:fhd办公区 - 实用经验教程分享!

    Java 给Word每一页设置不同文字水印效果fhd办公区 - 实用经验教程分享!

  • 2该信息未经授权抓取自百度经验
  • 3

    fhd办公区 - 实用经验教程分享!

    勾选选项,点击“Apply”,然后等待程序安装导入jar:fhd办公区 - 实用经验教程分享!

    Java 给Word每一页设置不同文字水印效果fhd办公区 - 实用经验教程分享!

  • Java代码

  • 1

    import com.spire.doc.*;

    fhd办公区 - 实用经验教程分享!

    import com.spire.doc.documents.*;

    fhd办公区 - 实用经验教程分享!

    import com.spire.doc.fields.ShapeObject;

    fhd办公区 - 实用经验教程分享!

    fhd办公区 - 实用经验教程分享!

    import java.awt.*;

    fhd办公区 - 实用经验教程分享!

    fhd办公区 - 实用经验教程分享!

    public class DifferentTextWatermark {

    fhd办公区 - 实用经验教程分享!

    public static void main(String[] args) {

    fhd办公区 - 实用经验教程分享!

    //加载Word测试文档

    fhd办公区 - 实用经验教程分享!

    Document doc = new Document();

    fhd办公区 - 实用经验教程分享!

    doc.loadFromFile("test.docx");

    fhd办公区 - 实用经验教程分享!

    fhd办公区 - 实用经验教程分享!

    //获取文档第一节

    fhd办公区 - 实用经验教程分享!

    Section section1 = doc.getSections().get(0);

    fhd办公区 - 实用经验教程分享!

    fhd办公区 - 实用经验教程分享!

    //定义水印文字的纵向坐标位置

    fhd办公区 - 实用经验教程分享!

    float y = (float) (section1.getPageSetup().getPageSize().getHeight()/3);

    fhd办公区 - 实用经验教程分享!

    fhd办公区 - 实用经验教程分享!

    //添加文字水印1

    fhd办公区 - 实用经验教程分享!

    HeaderFooter header1 = section1.getHeadersFooters().getHeader();//获取页眉

    fhd办公区 - 实用经验教程分享!

    header1.getParagraphs().clear();//删除原有页眉格式的段落

    fhd办公区 - 实用经验教程分享!

    Paragraph para1= header1.addParagraph();//重新添加段落

    fhd办公区 - 实用经验教程分享!

    //添加艺术字并设置大小

    fhd办公区 - 实用经验教程分享!

    ShapeObject shape1 = new ShapeObject(doc, ShapeType.Text_Plain_Text);

    fhd办公区 - 实用经验教程分享!

    shape1.setWidth(362);

    fhd办公区 - 实用经验教程分享!

    shape1.setHeight(118);

    fhd办公区 - 实用经验教程分享!

    //设置艺术字文本内容、位置及样式(即文本水印字样)

    fhd办公区 - 实用经验教程分享!

    shape1.setRotation(315);

    fhd办公区 - 实用经验教程分享!

    shape1.getWordArt().setText("内部使用");

    fhd办公区 - 实用经验教程分享!

    shape1.setFillColor(new Color(128,128,128));

    fhd办公区 - 实用经验教程分享!

    shape1.setLineStyle(ShapeLineStyle.Single);

    fhd办公区 - 实用经验教程分享!

    shape1.setStrokeColor(new Color(128,128,128));

    fhd办公区 - 实用经验教程分享!

    shape1.setStrokeWeight(0.5);

    fhd办公区 - 实用经验教程分享!

    shape1.setVerticalPosition(y);

    fhd办公区 - 实用经验教程分享!

    shape1.setHorizontalAlignment(ShapeHorizontalAlignment.Center);

    fhd办公区 - 实用经验教程分享!

    para1.getChildObjects().add(shape1);

    fhd办公区 - 实用经验教程分享!

    fhd办公区 - 实用经验教程分享!

    //同理设置第二节页眉中的文字水印2

    fhd办公区 - 实用经验教程分享!

    Section section2 = doc.getSections().get(1);

    fhd办公区 - 实用经验教程分享!

    HeaderFooter header2 = section2.getHeadersFooters().getHeader();

    fhd办公区 - 实用经验教程分享!

    header2.getParagraphs().clear();

    fhd办公区 - 实用经验教程分享!

    Paragraph para2= header2.addParagraph();

    fhd办公区 - 实用经验教程分享!

    ShapeObject shape2 = new ShapeObject(doc, ShapeType.Text_Plain_Text);

    fhd办公区 - 实用经验教程分享!

    shape2.setWidth(362);

    fhd办公区 - 实用经验教程分享!

    shape2.setHeight(118);

    fhd办公区 - 实用经验教程分享!

    shape2.setRotation(315);

    fhd办公区 - 实用经验教程分享!

    shape2.getWordArt().setText("绝密资料");

    fhd办公区 - 实用经验教程分享!

    shape2.setFillColor(new Color(221,160,221));

    fhd办公区 - 实用经验教程分享!

    shape2.setLineStyle(ShapeLineStyle.Single);

    fhd办公区 - 实用经验教程分享!

    shape2.setStrokeColor(new Color(221,160,221));

    fhd办公区 - 实用经验教程分享!

    shape2.setStrokeWeight(0.5);

    fhd办公区 - 实用经验教程分享!

    shape2.setVerticalPosition(y);

    fhd办公区 - 实用经验教程分享!

    shape2.setHorizontalAlignment(ShapeHorizontalAlignment.Center);

    fhd办公区 - 实用经验教程分享!

    para2.getChildObjects().add(shape2);

    fhd办公区 - 实用经验教程分享!

    fhd办公区 - 实用经验教程分享!

    //同理设置第三节中的页眉中的文字水印3

    fhd办公区 - 实用经验教程分享!

    Section section3 = doc.getSections().get(2);

    fhd办公区 - 实用经验教程分享!

    HeaderFooter header3 = section3.getHeadersFooters().getHeader();

    fhd办公区 - 实用经验教程分享!

    header3.getParagraphs().clear();

    fhd办公区 - 实用经验教程分享!

    Paragraph para3= header3.addParagraph();

    fhd办公区 - 实用经验教程分享!

    ShapeObject shape3 = new ShapeObject(doc, ShapeType.Text_Plain_Text);

    fhd办公区 - 实用经验教程分享!

    shape3.setWidth(362);

    fhd办公区 - 实用经验教程分享!

    shape3.setHeight(118);

    fhd办公区 - 实用经验教程分享!

    shape3.setRotation(315);

    fhd办公区 - 实用经验教程分享!

    shape3.getWordArt().setText("禁止传阅");

    fhd办公区 - 实用经验教程分享!

    shape3.setFillColor(new Color(70,130,180));

    fhd办公区 - 实用经验教程分享!

    shape3.setLineStyle(ShapeLineStyle.Single);

    fhd办公区 - 实用经验教程分享!

    shape3.setStrokeColor(new Color(70,130,180));

    fhd办公区 - 实用经验教程分享!

    shape3.setStrokeWeight(0.5);

    fhd办公区 - 实用经验教程分享!

    shape3.setVerticalPosition(y);

    fhd办公区 - 实用经验教程分享!

    shape3.setHorizontalAlignment(ShapeHorizontalAlignment.Center);

    fhd办公区 - 实用经验教程分享!

    para3.getChildObjects().add(shape3);

    fhd办公区 - 实用经验教程分享!

    fhd办公区 - 实用经验教程分享!

    //保存文档

    fhd办公区 - 实用经验教程分享!

    doc.saveToFile("DifferentTextWatermark.docx",FileFormat.Docx_2013);

    fhd办公区 - 实用经验教程分享!

    doc.dispose();

    fhd办公区 - 实用经验教程分享!

    }

    fhd办公区 - 实用经验教程分享!

    }fhd办公区 - 实用经验教程分享!

  • 2

    执行代码后,生成的Word文档中,可查看每一页的文字水印效果,如图:fhd办公区 - 实用经验教程分享!

    Java 给Word每一页设置不同文字水印效果fhd办公区 - 实用经验教程分享!

  • 以上方法由办公区教程网编辑摘抄自百度经验可供大家参考!fhd办公区 - 实用经验教程分享!


    标签: JAVAWord

    办公区 Copyright © 2016-2023 www.bgqu.net. Some Rights Reserved. 备案号:湘ICP备2020019561号统计代码