zoukankan      html  css  js  c++  java
  • Java 在Excel中添加水印(单一水印、平铺水印)

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

    程序环境:

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

    Java代码

    1.单一水印效果

    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;
        }
    }

    单一水印效果:

    2.平铺水印效果

    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;
        }
    }

    平铺水印效果:

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

    原创内容,如需转载,请务必注明出处

  • 相关阅读:
    前端页面性能参数搜集
    前端性能优化的另一种方式——HTTP2.0
    邮件服务器Postfix的管理 重启php-fpm
    腾讯云域名注册服务
    centos lamp/lnmp阶段复习 以后搬迁discuz论坛不需要重新安装,只需修改配置文件即可 安装wordpress 安装phpmyadmin 定时备份mysql两种方法 第二十五节课
    atime、mtime、ctime的区别及如何降低atime更新 mount时的option noatime
    centos LNMP第一部分环境搭建 LAMP LNMP安装先后顺序 php安装 安装nginx 编写nginx启动脚本 懒汉模式 mv /usr/php/{p.conf.default,p.conf} php运行方式SAPI介绍 第二十三节课
    centos LAMP第四部分mysql操作 忘记root密码 skip-innodb 配置慢查询日志 mysql常用操作 mysql常用操作 mysql备份与恢复 第二十二节课
    centos LAMP第三部分php,mysql配置 php配置文件 配置php的error_log 配置php的open_basedir 安装php的扩展模块 phpize mysql配置第二十一节课
    LVM的一些问题汇总 tune2fs命令
  • 原文地址:https://www.cnblogs.com/Yesi/p/14680209.html
Copyright © 2011-2022 走看看