日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

在Java生成的html页面加水印,Java在Excel中添加水印的实现(单一水印、平铺水印)...

發布時間:2025/3/15 java 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在Java生成的html页面加水印,Java在Excel中添加水印的实现(单一水印、平铺水印)... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在Excel中沒有直接添加水印的功能,但依舊可以通過一定方式來實現類似水印效果。本文通過Java程序代碼介紹具體實現方法。可添加單一水印效果,即水印是以單個文本字樣來呈現;也可添加多個平鋪水印效果,即水印是以多個文本字樣來頁面中平鋪。詳細內容見下文。

程序環境:

測試文檔:Office Excel 2013

編譯環境:IntelliJ IDEA 2018

JDK版本:1.8.0

Excel庫:Java系列free spire.xls.jar 3.9.1

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

{

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

{

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中添加水印(單一水印、平鋪水印)的文章就介紹到這了,更多相關Java 在Excel中添加水印(單一水印、平鋪水印)內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!

總結

以上是生活随笔為你收集整理的在Java生成的html页面加水印,Java在Excel中添加水印的实现(单一水印、平铺水印)...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。