You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
178 lines
7.7 KiB
178 lines
7.7 KiB
3 years ago
|
/**
|
||
|
* Copyright (C), 2015-2021
|
||
|
* FileName: WaterMarkHandler
|
||
|
* Author: Louis
|
||
|
* Date: 2020/3/16 21:55
|
||
|
* Description: WaterMarkHandler
|
||
|
* History:
|
||
|
* <author> <time> <version> <desc>
|
||
|
*/
|
||
|
package com.fr.plugin.ifhd.utils;
|
||
|
|
||
|
import com.fanruan.api.log.LogKit;
|
||
|
import com.fr.base.iofile.attr.WatermarkAttr;
|
||
|
import com.fr.io.exporter.poi.wrapper.POIWorkbookAction;
|
||
|
import com.fr.main.workbook.ResultWorkBook;
|
||
|
import com.fr.plugin.context.PluginContexts;
|
||
|
import com.fr.report.core.ReportUtils;
|
||
|
import com.fr.third.v2.org.apache.poi.ss.usermodel.Workbook;
|
||
|
import com.fr.third.v2.org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
||
|
import com.fr.third.v2.org.apache.poi.xssf.usermodel.XSSFRelation;
|
||
|
import com.fr.third.v2.org.apache.poi.xssf.usermodel.XSSFSheet;
|
||
|
import com.fr.third.v2.org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||
|
|
||
|
import javax.imageio.ImageIO;
|
||
|
import java.awt.*;
|
||
|
import java.awt.font.FontRenderContext;
|
||
|
import java.awt.geom.Rectangle2D;
|
||
|
import java.awt.image.BufferedImage;
|
||
|
import java.io.ByteArrayOutputStream;
|
||
|
import java.io.File;
|
||
|
import java.io.FileOutputStream;
|
||
|
import java.io.IOException;
|
||
|
|
||
|
public class WaterMarkHandler {
|
||
|
private final String WATER_MARK;
|
||
|
|
||
|
public WaterMarkHandler(String water_mark) {
|
||
|
WATER_MARK = water_mark;
|
||
|
}
|
||
|
|
||
|
public static ByteArrayOutputStream createWaterMark(WatermarkAttr watermarkAttr) throws IOException {
|
||
|
Color color = new Color(watermarkAttr.getColor().getRed(), watermarkAttr.getColor().getGreen(), watermarkAttr.getColor().getBlue(), 100);
|
||
|
int width = 300;
|
||
|
int height = 150;
|
||
|
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);// 获取bufferedImage对象
|
||
|
String fontType = "仿宋";
|
||
|
int fontStyle = Font.BOLD;
|
||
|
int fontSize = watermarkAttr.getFontSize();
|
||
|
Font font = new Font(fontType, fontStyle, fontSize);
|
||
|
Graphics2D g2d = image.createGraphics(); // 获取Graphics2d对象
|
||
|
image = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
|
||
|
g2d.dispose();
|
||
|
g2d = image.createGraphics();
|
||
|
g2d.setColor(color); //设置字体颜色和透明度,最后一个参数为透明度
|
||
|
g2d.setStroke(new BasicStroke(1)); // 设置字体
|
||
|
g2d.setFont(font); // 设置字体类型 加粗 大小
|
||
|
g2d.rotate(-0.3, (double) image.getWidth() / 2, (double) image.getHeight() / 2);//设置倾斜度
|
||
|
FontRenderContext context = g2d.getFontRenderContext();
|
||
|
Rectangle2D bounds = font.getStringBounds(watermarkAttr.getText(), context);
|
||
|
double x = (width - bounds.getWidth()) / 2;
|
||
|
double y = (height - bounds.getHeight()) / 2;
|
||
|
double ascent = -bounds.getY();
|
||
|
double baseY = y + ascent;
|
||
|
// 写入水印文字原定高度过小,所以累计写水印,增加高度
|
||
|
String[] info = watermarkAttr.getText().split("\\;");
|
||
|
if (info.length > 0) {
|
||
|
for (int i = 0; i < info.length; i++) {
|
||
|
String s = info[i];
|
||
|
g2d.drawString(s, 1 + (i * 40), (int) baseY + (i * 25));
|
||
|
}
|
||
|
} else {
|
||
|
g2d.drawString(watermarkAttr.getText(), 0, (int) baseY);
|
||
|
}
|
||
|
|
||
|
// 设置透明度
|
||
|
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
|
||
|
// 释放对象
|
||
|
g2d.dispose();
|
||
|
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||
|
ImageIO.write(image, "png", os);
|
||
|
return os;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 为Excel打上水印工具函数
|
||
|
*
|
||
|
* @param sheet excel sheet
|
||
|
* @param bytes 水印图片字节数组
|
||
|
*/
|
||
|
public static void putWaterRemarkToExcel(XSSFSheet sheet, byte[] bytes) {
|
||
|
//add relation from sheet to the picture data
|
||
|
XSSFWorkbook workbook = sheet.getWorkbook();
|
||
|
int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
|
||
|
String rID = sheet.addRelation(null, XSSFRelation.IMAGES, workbook.getAllPictures().get(pictureIdx))
|
||
|
.getRelationship().getId();
|
||
|
//set background picture to sheet
|
||
|
sheet.getCTWorksheet().addNewPicture().setId(rID);
|
||
|
}
|
||
|
|
||
|
public static void main(String[] args) throws IOException {
|
||
|
FileOutputStream fileOutputStream = new FileOutputStream(new File("D:\\aa.xlsx"));
|
||
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
||
|
XSSFSheet sheet = workbook.createSheet("Sheet1");
|
||
|
WatermarkAttr watermarkAttr = new WatermarkAttr();
|
||
|
watermarkAttr.setText("内部信息,严禁外传;2021-09-14;曲君阳");
|
||
|
watermarkAttr.setFontSize(16);
|
||
|
watermarkAttr.setColor(new Color(0, 0, 0, 100));
|
||
|
try (ByteArrayOutputStream byteArrayOutputStream = WaterMarkHandler.createWaterMark(watermarkAttr)) {
|
||
|
int pictureIdx = workbook.addPicture(byteArrayOutputStream.toByteArray(), Workbook.PICTURE_TYPE_PNG);
|
||
|
//add relation from sheet to the picture data
|
||
|
String rID = sheet.addRelation(null, XSSFRelation.IMAGES, workbook.getAllPictures().get(pictureIdx))
|
||
|
.getRelationship().getId();
|
||
|
//set background picture to sheet
|
||
|
sheet.getCTWorksheet().addNewPicture().setId(rID);
|
||
|
workbook.write(fileOutputStream);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void setWartMark(XSSFWorkbook workbook, XSSFSheet sheet, WatermarkAttr wartInfo) {
|
||
|
try (ByteArrayOutputStream byteArrayOutputStream = WaterMarkHandler.createWaterMark(wartInfo)) {
|
||
|
int pictureIdx = workbook.addPicture(byteArrayOutputStream.toByteArray(), Workbook.PICTURE_TYPE_PNG);
|
||
|
//add relation from sheet to the picture data
|
||
|
String rID = sheet.addRelation(null, XSSFRelation.IMAGES, workbook.getAllPictures().get(pictureIdx))
|
||
|
.getRelationship().getId();
|
||
|
//set background picture to sheet
|
||
|
sheet.getCTWorksheet().addNewPicture().setId(rID);
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 设定水印图片Idx
|
||
|
*
|
||
|
* @param resultWorkBook
|
||
|
* @param poiWorkbookAction
|
||
|
* @return
|
||
|
*/
|
||
|
private static int getPictureIdx(ResultWorkBook resultWorkBook, POIWorkbookAction poiWorkbookAction) {
|
||
|
WatermarkAttr watermarkAttr = ReportUtils.getWatermarkAttrFromTemplate(resultWorkBook);
|
||
|
if (watermarkAttr == null || watermarkAttr.isEmpty()) {
|
||
|
return 0;
|
||
|
}
|
||
|
try {
|
||
|
LogKit.info("ifhd-WaterMarkHandler-watermarkAttr:{}", watermarkAttr.getText());
|
||
|
ByteArrayOutputStream byteArrayOutputStream = createWaterMark(watermarkAttr);
|
||
|
return poiWorkbookAction.addPicture(byteArrayOutputStream.toByteArray(), Workbook.PICTURE_TYPE_PNG);
|
||
|
} catch (IOException e) {
|
||
|
LogKit.error(e.getMessage(), e);
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 关联sheet和水印图片
|
||
|
*
|
||
|
* @param sxssfWorkbook
|
||
|
*/
|
||
|
private static void sheetAddPicture(SXSSFWorkbook sxssfWorkbook, int pictureIdx) {
|
||
|
if (!PluginContexts.currentContext().isAvailable()) {
|
||
|
return;
|
||
|
}
|
||
|
XSSFSheet xssfSheet;
|
||
|
String rID;
|
||
|
XSSFWorkbook xssfWorkbook = sxssfWorkbook.getXSSFWorkbook();
|
||
|
for (int j = 0; j < xssfWorkbook.getNumberOfSheets(); j++) {
|
||
|
xssfSheet = xssfWorkbook.getSheetAt(j);
|
||
|
rID = xssfSheet.addRelation(null, XSSFRelation.IMAGES, xssfWorkbook.getAllPictures().get(pictureIdx))
|
||
|
.getRelationship().getId();
|
||
|
xssfSheet.getCTWorksheet().addNewPicture().setId(rID);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void putWaterRemarkToExcel(ResultWorkBook resultWorkBook, POIWorkbookAction poiWorkbookAction) {
|
||
|
int pictureIdx = getPictureIdx(resultWorkBook, poiWorkbookAction);
|
||
|
sheetAddPicture((SXSSFWorkbook) poiWorkbookAction.getWorkbook(), pictureIdx);
|
||
|
}
|
||
|
}
|