Java在Excel中添加水印的实现(单一水印、平铺水印)

2025-05-29 0 32

Excel中没有直接添加水印的功能,但依旧可以通过一定方式来实现类似水印效果。本文通过Java程序代码介绍具体实现方法。可添加单一水印效果,即水印是以单个文本字样来呈现;也可添加多个平铺水印效果,即水印是以多个文本字样来页面中平铺。详细内容见下文。

程序环境:

  • 测试文档:Office Excel 2013
  • 编译环境:IntelliJ IDEA 2018
  • JDK版本:1.8.0
  • Excel库:Java系列free spire.xls.jar 3.9.1

Java代码

1.单一水印效果

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60
import com.spire.xls.*;

import java.awt.*;

import java.awt.image.BufferedImage;

import static java.awt.image.BufferedImage.TYPE_INT_ARGB;

public class SingleWatermark {

public static void main(String[] args) {

//加载Excel测试文档

Workbook wb = new Workbook();

wb.loadFromFile("test.xlsx");

//设置文本和字体大小

Font font = new Font("仿宋", Font.PLAIN, 40);

for (int i =0;i<wb.getWorksheets().getCount();i++)

{

Worksheet sheet = wb.getWorksheets().get(i);

//调用DrawText() 方法插入图片

BufferedImage imgWtrmrk = drawText("内部专用", font, Color.pink, Color.white, sheet.getPageSetup().getPageHeight(), sheet.getPageSetup().getPageWidth());

//将图片设置为页眉

sheet.getPageSetup().setCenterHeaderImage(imgWtrmrk);

sheet.getPageSetup().setCenterHeader("&G");

//将显示模式设置为Layout

sheet.setViewMode(ViewMode.Layout);

}

//保存文档

wb.saveToFile("SingleWatermark.xlsx", ExcelVersion.Version2013);

}

private static BufferedImage drawText (String text, Font font, Color textColor, Color backColor,double height, double width)

{

//定义图片宽度和高度

BufferedImage img = new BufferedImage((int) width, (int) height, TYPE_INT_ARGB);

Graphics2D loGraphic = img.createGraphics();

//获取文本size

FontMetrics loFontMetrics = loGraphic.getFontMetrics(font);

int liStrWidth = loFontMetrics.stringWidth(text);

int liStrHeight = loFontMetrics.getHeight();

//文本显示样式及位置

loGraphic.setColor(backColor);

loGraphic.fillRect(0, 0, (int) width, (int) height);

loGraphic.translate(((int) width - liStrWidth) / 2, ((int) height - liStrHeight) / 2);

loGraphic.rotate(Math.toRadians(-45));

loGraphic.translate(-((int) width - liStrWidth) / 2, -((int) height - liStrHeight) / 2);

loGraphic.setFont(font);

loGraphic.setColor(textColor);

loGraphic.drawString(text, ((int) width - liStrWidth) / 2, ((int) height - liStrHeight) / 2);

loGraphic.dispose();

return img;

}

}

单一水印效果:

Java在Excel中添加水印的实现(单一水印、平铺水印)

2.平铺水印效果

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61
import com.spire.xls.*;

import java.awt.*;

import java.awt.image.BufferedImage;

import static java.awt.image.BufferedImage.TYPE_INT_ARGB;

public class TiledWatermark {

public static void main(String[] args) {

//加载Excel测试文档

Workbook wb = new Workbook();

wb.loadFromFile("test.xlsx");

//设置文本和字体大小

Font font = new Font("仿宋", Font.PLAIN, 25);

for (int i =0;i<wb.getWorksheets().getCount();i++)

{

Worksheet sheet = wb.getWorksheets().get(i);

//调用DrawText() 方法插入图片

BufferedImage imgWtrmrk = drawText("内部专用 内部专用 内部专用 内部专用", font, Color.pink, Color.white, sheet.getPageSetup().getPageHeight(), sheet.getPageSetup().getPageWidth());

//将图片设置为页眉

sheet.getPageSetup().setCenterHeaderImage(imgWtrmrk);

sheet.getPageSetup().setCenterHeader("&G");

//将显示模式设置为Layout

sheet.setViewMode(ViewMode.Layout);

}

//保存文档

wb.saveToFile("TiledWatermark.xlsx", ExcelVersion.Version2013);

}

private static BufferedImage drawText (String text, Font font, Color textColor, Color backColor,double height, double width)

{

//定义图片宽度和高度

BufferedImage img = new BufferedImage((int) width, (int) height, TYPE_INT_ARGB);

Graphics2D loGraphic = img.createGraphics();

//获取文本size

FontMetrics loFontMetrics = loGraphic.getFontMetrics(font);

int liStrWidth = loFontMetrics.stringWidth(text);

int liStrHeight = loFontMetrics.getHeight();

//文本显示样式及位置

loGraphic.setColor(backColor);

loGraphic.fillRect(0, 0, (int) width, (int) height);

loGraphic.translate(((int) width - liStrWidth) / 2, ((int) height - liStrHeight) / 2);

//loGraphic.rotate(Math.toRadians(-45));

loGraphic.translate(-((int) width - liStrWidth) / 2, -((int) height - liStrHeight) / 2);

loGraphic.setFont(font);

loGraphic.setColor(textColor);

loGraphic.drawString(text, ((int) width - liStrWidth) /6 , ((int) height - liStrHeight) /6);

loGraphic.drawString(text,((int) width - liStrWidth) /3, ((int) height - liStrHeight) /3);

loGraphic.drawString(text,((int) width - liStrWidth) /2, ((int) height - liStrHeight) /2);

loGraphic.dispose();

return img;

}

}

平铺水印效果:

Java在Excel中添加水印的实现(单一水印、平铺水印)

★ 需要注意的是:在添加完水印效果后,查看文档时,在“普通视图”水印不可见,需在“页面布局”模式或“打印预览”模式下查看。


到此这篇关于JavaExcel中添加水印(单一水印、平铺水印)的文章就介绍到这了,更多相关JavaExcel中添加水印(单一水印、平铺水印)内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!

原文链接:https://www.cnblogs.com/Yesi/p/14680209.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java在Excel中添加水印的实现(单一水印、平铺水印) https://www.kuaiidc.com/104134.html

相关文章

发表评论
暂无评论