Java 在Excel中新增水印(單一水印、平鋪水印)

iceblue發表於2021-04-20

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

平鋪水印效果:

 

★ 需要注意的是:在新增完水印效果後,檢視文件時,在“普通檢視”水印不可見,需在“頁面佈局”模式或“列印預覽”模式下檢視。

 

原創內容,如需轉載,請務必註明出處

 

相關文章