使用idea开发,所需依赖如下:

spire的下载、使用,代码中会给出网址。idea中选中右键,添加为库即可使用

        <!--使用spire,导入的jar-->        <dependency><groupId>e-iceblue</groupId><artifactId>spire.office.free</artifactId><version>5.3.1</version><scope>system</scope><systemPath>${project.basedir}/lib/spire.office.free-5.3.1.jar</systemPath></dependency><dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.25</version></dependency><dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.13</version></dependency><!--压缩图片,保持原像素--><dependency><groupId>org.imgscalr</groupId><artifactId>imgscalr-lib</artifactId><version>4.2</version></dependency>

还需要一张白色图片,作用在于可以设置水印图片的透明度(spire没有提供方法)

具体代码如下:

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.spire.doc.*;
import com.spire.doc.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.PdfPageSize;
import com.spire.pdf.graphics.PdfMargins;
import com.spire.presentation.Presentation;
import com.spire.presentation.collections.SlideCollection;
import com.spire.presentation.drawing.BackgroundType;
import com.spire.presentation.drawing.FillFormatType;
import com.spire.presentation.drawing.IImageData;
import com.spire.presentation.drawing.PictureFillType;
import com.spire.xls.ExcelVersion;
import com.spire.xls.ViewMode;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import com.spire.xls.collections.WorksheetsCollection;
import com.wuwei.common.config.WuWeiConfig;import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.io.MemoryUsageSetting;
import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;
import org.imgscalr.Scalr;
import org.springframework.core.io.ClassPathResource;import javax.imageio.ImageIO;import static java.awt.image.BufferedImage.TYPE_INT_ARGB;/*** jar下载地址:https://repo.e-iceblue.cn/#browse/browse:maven-public:e-iceblue,下载free的;* <p>* 使用文档:https://www.e-iceblue.cn/tutorials.html,查看...for java的文档* <p>* 为下列文件添加图片水印:* <p>* word;* <p>* excel:本质上是添加图片页眉,且在正常模式下不可见,仅在页面布局模式或打印预览模式可见;也可添加背景图片;* <p>* ppt:本质是添加背景图片;* <p>* pdf;*/
@Slf4j
public class WaterMarkUtils {/*** 100为原大小;* word水印缩放大小;*/public static final int SIZE = 100;/*** 当前模块resource下的静态图片->设置透明度的背景图片*/public static final String BACKGROUND_PATH = "static/" + "white.png";/*** 仅针对excel文件:* 水印图片透明度设置:0->1.0f,逐渐不透明;*/public static final Float ALPHA_EXCEL = 0.2f;/*** 仅针对ppt文件:* 水印图片透明度设置:0->1.0f,逐渐不透明;*/public static final Float ALPHA_PPT = 0.2f;/*** 仅针对pdf文件:* 水印图片透明度设置:0->1.0f,逐渐不透明;*/public static final Float ALPHA_PDF = 0.2f;/*** 仅针对图片:* 水印图片透明度设置:0->1.0f,逐渐不透明;*/public static final Float ALPHA_IMG = 0.4f;/*** 对水印图片进行缩放的大小设置:* 大于370->等比缩小到370;* 小于370->等比放大到370;*/public static final int TARGETWIDTH = 370;/*** 对水印图片进行缩放的大小设置:* 大于370->等比缩小到370;* 小于370->等比放大到370;*/public static final int TARGETHEIGHT = 370;/*** "doc", "docx", "xls", "xlsx", "ppt", "pptx", "pdf"*/public static final String WORD_TYPE = "word";public static final String DOC = "doc";public static final String DOCX = "docx";public static final String EXCEL_TYPE = "excel";public static final String XLS = "xls";public static final String XLSX = "xlsx";public static final String PPT_TYPE = "ppt";public static final String PPT = "ppt";public static final String PPTX = "pptx";public static final String PDF_TYPE = "pdf";public static final String PDF = "pdf";public static final String IMG_TYPE = "img";public static final String[] IMG = {"bmp", "gif", "jpg", "jpeg", "png"};/*** 复制文件并添加水印** @param filePath          下载文件的绝对路径;* @param waterMarkFilePath 水印图片的路径,直接redisCache.getCacheObject(WATER_MARK_FILE_PATH))获取;* @return 1.已添加水印文件的绝对路径,再行下载,下载后需自行删除该水印文件;* <p>* 2.为空,代表不是添加水印的文件,按原路径下载;*/public static String addWaterMark(String filePath, String waterMarkFilePath) {String path = "";//判断是否有水印图片if (!new File(waterMarkFilePath).exists()) {log.info("abc-" + "水印图片不存在");return path;}String fileType = getFileType(filePath);try {if (fileType.equals(WORD_TYPE)) {//word文件path = wordAddWaterMark(filePath, waterMarkFilePath);} else if (fileType.equals(EXCEL_TYPE)) {//excel文件path = excelAddWaterMark1(filePath, waterMarkFilePath);} else if (fileType.equals(PPT_TYPE)) {//PPT文件path = pptAddWaterMark(filePath, waterMarkFilePath);} else if (fileType.equals(PDF_TYPE)) {//PDF文件path = pdfAddWaterMark2(filePath, waterMarkFilePath);} else if (fileType.equals(IMG_TYPE)) {//图片path = imgAddWaterMark(filePath, waterMarkFilePath);}} catch (Exception e) {log.error("文件添加水印失败" + e.getMessage());e.printStackTrace();}return path;}/*** 为图片添加水印** @param filePath* @param waterMarkFilePath* @return*/private static String imgAddWaterMark(String filePath, String waterMarkFilePath) throws IOException {//获取画布BufferedImage read = ImageIO.read(new File(filePath));Graphics2D graphics = read.createGraphics();//缩放水印图片BufferedImage image = ImageIO.read(new File(waterMarkFilePath));int width = image.getWidth();int height = image.getHeight();//设置比例float f = getScale(width, height, 0.5f);//获取缩放后的宽高int w = (int) (width * f);int h = (int) (height * f);//缩放图片Image i = image.getScaledInstance(w, h, Image.SCALE_SMOOTH);//设置透明度,0->1,逐渐不透明graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA_IMG));//添加水印并设置在图片的右下角graphics.drawImage(i, read.getWidth() - w, read.getHeight() - h, null);//释放资源graphics.dispose();//保存图片String downloadPath = filePath.substring(0, filePath.lastIndexOf("/") + 1) +System.currentTimeMillis() + "_" + StrUtil.subAfter(filePath, "/", true);ImageIO.write(read, filePath.substring(filePath.lastIndexOf(".") + 1), new File(downloadPath));return downloadPath;}public static float getScale(int width, int height, float f) {if (width > 3000) {f = 0.06f;}if (width > 1000 && width < 3000) {f = 0.1f;}if (width > 300 && width < 1000) {f = 0.3f;}return f;}/*** pdf文件添加水印,使用itextpdf,速度最快** @param filePath* @param waterMarkFilePath* @return*/public static String pdfAddWaterMark2(String filePath, String waterMarkFilePath) throws Exception {PdfReader reader = new PdfReader(filePath);String downloadPath = filePath.substring(0, filePath.lastIndexOf("/") + 1) +System.currentTimeMillis() + "_" + StrUtil.subAfter(filePath, "/", true);PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(downloadPath));PdfGState gs1 = new PdfGState();// 设置透明度gs1.setFillOpacity(ALPHA_PDF);//得到缩放后的图片String resizePath = resizeImage(waterMarkFilePath, TARGETWIDTH, TARGETHEIGHT);com.itextpdf.text.Image image = com.itextpdf.text.Image.getInstance(resizePath);// 获取PDF页数int num = reader.getNumberOfPages();com.itextpdf.text.Rectangle pagesize;float width = 0f;float height = 0f;//设置缩放后的宽高/*float scale = 0.5f;if (image.getWidth() < 200 && image.getHeight() < 200) {scale = 2f;}float w = image.getWidth() * scale;float h = image.getHeight() * scale;*/int x = 0;for (int i = 1; i <= num; i++) {//得到页面大小x++;if (x == 1) {pagesize = reader.getPageSize(i);width = pagesize.getWidth();height = pagesize.getHeight();}//水印图片设置在内容之上,之下用getUnderContentPdfContentByte pdfContentByte = stamper.getOverContent(i);pdfContentByte.setGState(gs1);//设置图片的位置,参数Image.UNDERLYING是作为文字的背景显示。image.setAlignment(com.itextpdf.text.Image.UNDERLYING);//设置图片缩放比例//image.scaleToFit(w, h);//设置图片的大小//image.scaleAbsolute(200, 200);//设置图片的绝对位置image.setAbsolutePosition((width - image.getWidth()) / 2, (height - image.getHeight()) / 2);pdfContentByte.addImage(image);}stamper.close();reader.close();//删除缩放后的图片FileUtil.del(resizePath);return downloadPath;}/*** pdf文件添加水印,使用pdfbox,速度一般** @param filePath* @param waterMarkFilePath* @return*/private static String pdfAddWaterMark1(String filePath, String waterMarkFilePath) throws Exception {// 添加水印后的文件路径String downloadPath = filePath.substring(0, filePath.lastIndexOf("/") + 1) +System.currentTimeMillis() + "_" + StrUtil.subAfter(filePath, "/", true);// 加载pdf文件PDDocument lod = PDDocument.load(new FileInputStream(filePath));for (int i = 0; i < lod.getNumberOfPages(); i++) {PDPage page = lod.getPage(i);PDPageContentStream contentStream = new PDPageContentStream(lod, page, PDPageContentStream.AppendMode.APPEND, true, true);PDImageXObject pdImage = PDImageXObject.createFromFile(waterMarkFilePath, lod);// 设置透明度PDExtendedGraphicsState pdExtGfxState = new PDExtendedGraphicsState();pdExtGfxState.setNonStrokingAlphaConstant(ALPHA_PDF);pdExtGfxState.setAlphaSourceFlag(true);pdExtGfxState.getCOSObject().setItem(COSName.BM, COSName.MULTIPLY);contentStream.setGraphicsStateParameters(pdExtGfxState);// 设置水印大小及位置contentStream.drawImage(pdImage, page.getMediaBox().getWidth() / 2 - 80,page.getMediaBox().getHeight() / 2 - 100, 200, 200);contentStream.close();lod.save(downloadPath);}lod.close();return downloadPath;}/*** pdf文件添加水印,使用spire添加,但免费版只能转换前十页,故采用PDFBox拆分合并,但速度很慢** @param filePath* @param waterMarkFilePath* @return*/private static String pdfAddWaterMark(String filePath, String waterMarkFilePath) throws Exception {// 创建临时文件夹String tmpFolder = "D:\\hams" + "\\tmp_wm_";String s = System.currentTimeMillis() + "_" + UUID.randomUUID().toString().replaceAll("-", "");new File(tmpFolder + s).mkdir();// 添加水印后的文件路径String downloadPath = filePath.substring(0, filePath.lastIndexOf("/") + 1) +System.currentTimeMillis() + "_" + StrUtil.subAfter(filePath, "/", true);// 加载pdf文件PDDocument lod = PDDocument.load(new File(filePath));// 设置页边距为左右0、上下0PdfMargins margins = new PdfMargins(0, 0);// 调整画布,设置内容也根据页面的大小进行缩放double wScale = (PdfPageSize.A4.getWidth() - 10) / PdfPageSize.A4.getWidth();double hScale = (PdfPageSize.A4.getHeight() - 10) / PdfPageSize.A4.getHeight();// 定义文件名存储ListList<String> inputPaths = new ArrayList<>();// 获取原来pdf的总页数int pageCount = lod.getPages().getCount();if (pageCount > 0) {// 判断需要生成几个pdf文件int n = (int) Math.ceil((double) pageCount / 10);int x = 0;// 每十页生成一个pdf文件for (int i = 1; i <= n; i++) {PDDocument pd = new PDDocument();// 遍历所有PDF页面for (int j = 0; j < 10; j++) {if (x < pageCount) {pd.addPage(lod.getPage(x));x++;}}// 创建新的pdf文件pd.save(tmpFolder + s + "\\" + i + ".pdf");// 保存文件存放路径inputPaths.add(tmpFolder + s + "\\" + i + ".pdf");}}// 关闭lod.close();// 执行添加图片水印,合并PDFint x = 1;List<String> outputPaths = new ArrayList<>();// 为拆分的PDF添加水印for (String path : inputPaths) {// 添加水印后的PDF保存路径String outputPath2 = tmpFolder + s + "\\" + "wm_" + x + ".pdf";outputPaths.add(outputPath2);x++;FileInputStream fis = new FileInputStream(path);//创建文档PdfDocument pdf = new PdfDocument(fis);//遍历所有PDF 页面for (int i = 0; i < pdf.getPages().getCount(); i++) {//添加一页PdfPageBase page = pdf.getPages().get(i);//设置水印位置和大小Rectangle2D.Float rect = new Rectangle2D.Float();rect.setRect(page.getClientSize().getWidth() / 2 - 80,page.getClientSize().getHeight() / 2 - 100,200, 200);page.setBackgroundImage(waterMarkFilePath);page.setBackgroundRegion(rect);}//保存添加水印后的PDFpdf.saveToFile(outputPath2);pdf.close();// 关闭fis.close();}// 合并添加好水印的PDFPDFMergerUtility mergePdf = new PDFMergerUtility();for (String outputPath3 : outputPaths) {File f = new File(outputPath3);if (f.exists() && f.isFile()) {// 循环添加要合并的pdfmergePdf.addSource(f);}}// 设置合并生成pdf文件名称mergePdf.setDestinationFileName(downloadPath);// 执行合并pdfmergePdf.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());// 删除所有临时文件File files = new File(tmpFolder + s);//判断是否为文件夹if (files.exists() && files.isDirectory()) {//获取该文件夹下的子文件夹File[] files2 = files.listFiles();//循环子文件夹重复调用delete方法for (int i = 0; i < files2.length; i++) {files2[i].delete();}files.delete();}return downloadPath;}/*** ppt文件添加水印,实际上只是添加背景图片** @param filePath* @param waterMarkFilePath* @return*/private static String pptAddWaterMark(String filePath, String waterMarkFilePath) throws Exception {Presentation presentation = new Presentation();presentation.loadFromFile(filePath);//获取设置了透明度的水印图片//File file = new File(waterMarkFilePath);String path = drawTransparent(waterMarkFilePath, ALPHA_PPT);File file = new File(path);IImageData image = presentation.getImages().append(ImageIO.read(file));//获取幻灯片背景属性,设置图片填充SlideCollection slides = presentation.getSlides();for (int i = 0; i < slides.size(); i++) {slides.get(i).getSlideBackground().setType(BackgroundType.CUSTOM);slides.get(i).getSlideBackground().getFill().setFillType(FillFormatType.PICTURE);slides.get(i).getSlideBackground().getFill().getPictureFill().setFillType(PictureFillType.STRETCH);slides.get(i).getSlideBackground().getFill().getPictureFill().getPicture().setEmbedImage(image);}//保存文档String downloadPath = filePath.substring(0, filePath.lastIndexOf("/") + 1) +System.currentTimeMillis() + "_" + StrUtil.subAfter(filePath, "/", true);presentation.saveToFile(downloadPath, com.spire.presentation.FileFormat.PPSX_2013);//删除设置了透明度的水印图片file.delete();return downloadPath;}/*** word文件添加水印** @param filePath* @param waterMarkFilePath* @return*/public static String wordAddWaterMark(String filePath, String waterMarkFilePath) throws Exception {Document document = new Document();document.loadFromFile(filePath);PictureWatermark picture = new PictureWatermark();//得到缩放后的图片String resizePath = resizeImage(waterMarkFilePath, TARGETWIDTH, TARGETHEIGHT);//设置图片picture.setPicture(resizePath);//设置图片大小picture.setScaling(SIZE);//是否冲刷,true->图片色彩变淡,false->原色彩picture.isWashout(true);document.setWatermark(picture);//保存文档String downloadPath = filePath.substring(0, filePath.lastIndexOf("/") + 1) +System.currentTimeMillis() + "_" + StrUtil.subAfter(filePath, "/", true);document.saveToFile(downloadPath, FileFormat.Docx);//删除缩放后的图片FileUtil.del(resizePath);return downloadPath;}/*** excel文件添加水印;* <p>* 本质上是添加背景图片** @param filePath* @param waterMarkFilePath* @return*/private static String excelAddWaterMark1(String filePath, String waterMarkFilePath) throws IOException {//创建Workbook实例Workbook workbook = new Workbook();//加载Excel文档workbook.loadFromFile(filePath);//得到设置了透明度的水印图片String path = drawTransparent(waterMarkFilePath, ALPHA_EXCEL);File file = new File(path);BufferedImage bufferedImage = ImageIO.read(file);//获取工作表WorksheetsCollection worksheets = workbook.getWorksheets();for (int i = 0; i < worksheets.size(); i++) {//将图片设置为工作表的背景图worksheets.get(i).getPageSetup().setBackgoundImage(bufferedImage);}//保存文档String downloadPath = filePath.substring(0, filePath.lastIndexOf("/") + 1) +System.currentTimeMillis() + "_" + StrUtil.subAfter(filePath, "/", true);workbook.saveToFile(downloadPath, ExcelVersion.Version2013);//删除设置了透明度的水印图片file.delete();return downloadPath;}/*** 绘制透明度图片** @param waterMarkFilePath 水印图片* @param alpha             透明度* @return* @throws IOException*/public static String drawTransparent(String waterMarkFilePath, float alpha) throws IOException {//读取背景图片ClassPathResource resource = new ClassPathResource(BACKGROUND_PATH);//使用resource.getFile();会报错,打成jar后,没办法通过File的形式访问jar包里面的文件,用流读取文件可以InputStream stream = resource.getInputStream();// 读取背景图片BufferedImage read = ImageIO.read(stream);Graphics2D graphics = read.createGraphics();//读取水印图片BufferedImage image = ImageIO.read(new File(waterMarkFilePath));//设置透明度,0->1,逐渐不透明graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));//添加水印并设置居中位置graphics.drawImage(image, (read.getWidth() - image.getWidth()) / 2, (read.getHeight() - image.getHeight()) / 2, null);//释放资源graphics.dispose();//保存图片String downloadPath = WuWeiConfig.getProfile() + "/" + System.currentTimeMillis() + "_" + StrUtil.subAfter(waterMarkFilePath, "/", true);ImageIO.write(read, waterMarkFilePath.substring(waterMarkFilePath.lastIndexOf(".") + 1), new File(downloadPath));return downloadPath;}/*** excel文件添加水印;* <p>* 本质上是添加图片页眉,并且水印在正常模式下不可见,仅在页面布局模式或打印预览模式可见。** @param filePath* @param waterMarkFilePath* @return*/private static String excelAddWaterMark(String filePath, String waterMarkFilePath) throws IOException {Workbook workbook = new Workbook();workbook.loadFromFile(filePath);for (Worksheet sheet : (Iterable<Worksheet>) workbook.getWorksheets()) {//添加水印BufferedImage img = drawText(waterMarkFilePath, sheet.getPageSetup().getPageHeight(), sheet.getPageSetup().getPageWidth());//设置页眉sheet.getPageSetup().setCenterHeaderImage(img);sheet.getPageSetup().setCenterHeader("&G");//设置边距sheet.getPageSetup().setTopMargin(0);sheet.getPageSetup().setLeftMargin(0);sheet.getPageSetup().setRightMargin(0);sheet.getPageSetup().setBottomMargin(0);//将显示模式设置为页面布局模式sheet.setViewMode(ViewMode.Layout);}//保存文档String downloadPath = filePath.substring(0, filePath.lastIndexOf("/") + 1) +System.currentTimeMillis() + "_" + StrUtil.subAfter(filePath, "/", true);workbook.saveToFile(downloadPath, ExcelVersion.Version2010);return downloadPath;}private static BufferedImage drawText(String waterMarkFilePath, double height, double width) throws IOException {//创建画布BufferedImage img = new BufferedImage((int) width, (int) height, TYPE_INT_ARGB);Graphics2D loGraphic = img.createGraphics();//使用ImageIO的read方法读取图片BufferedImage read = ImageIO.read(new File(waterMarkFilePath));//获取缩放后的宽高int w = (int) (read.getWidth() * 0.5);int h = (int) (read.getHeight() * 0.5);//调用缩放方法获取缩放后的图片Image i = read.getScaledInstance(w, h, Image.SCALE_DEFAULT);//画布上添加水印图片及设置水印位置loGraphic.drawImage(i, ((int) width - w) / 2 - 10, ((int) height - h) / 2, null);//释放资源loGraphic.dispose();return img;}private static String getFileType(String filePath) {int i = filePath.lastIndexOf(".");String str = filePath.substring(i + 1);switch (str) {case DOC:case DOCX:return WORD_TYPE;case XLS:case XLSX:return EXCEL_TYPE;case PPT:case PPTX:return PPT_TYPE;case PDF:return PDF_TYPE;}if (Arrays.asList(IMG).contains(str.toLowerCase())) {return IMG_TYPE;}return "";}/*** 画透明字符串图片** @param width      图片宽度* @param height     图片高度* @param fontHeight 字体大小* @param drawStr    文字* @return*/public static BufferedImage drawTransparentStringPic(int width, int height, Integer fontHeight, String drawStr) {try {BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics2D gd = buffImg.createGraphics();//设置透明buffImg = gd.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);gd = buffImg.createGraphics();//设置对线段的锯齿状边缘处理gd.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);//设置旋转(旋转角度,旋转x轴定点,y轴定点),右斜对角gd.rotate(Math.toRadians(-47), (width - fontHeight) / 2, height / 2);//设置字体gd.setFont(new Font("宋体", Font.BOLD, fontHeight));//设置颜色gd.setColor(Color.RED);//画边框//gd.drawRect(0, 0, width - 1, height - 1);//绘制文字gd.drawString(drawStr, (width - fontHeight * drawStr.length()) / 2, height / 2);gd.dispose();return buffImg;} catch (Exception e) {return null;}}/* public static void main(String[] args) {BufferedImage imgMap = drawTransparentStringPic(700, 700, 39, "奥术是否阿萨德");File imgFile = new File("D:\\hams\\asd789888.png");try {ImageIO.write(imgMap, "PNG", imgFile);} catch (IOException e) {e.printStackTrace();}System.out.println("生成完成");}*//*** https://www.baeldung.com/java-resize-image* 对水印图片进行缩放的大小设置:* 大于370->等比缩小到370;* 小于370->等比放大到370;** @param waterMarkFilePath 水印图片路径* @param targetWidth       缩放设置的宽* @param targetHeight      缩放设置的高* @return* @throws Exception*/public static String resizeImage(String waterMarkFilePath, int targetWidth, int targetHeight) throws Exception {BufferedImage originalImage = ImageIO.read(new File(waterMarkFilePath));BufferedImage resize = Scalr.resize(originalImage, Scalr.Method.AUTOMATIC, Scalr.Mode.AUTOMATIC, targetWidth, targetHeight, Scalr.OP_ANTIALIAS);//保存图片String downloadPath = WuWeiConfig.getProfile() + "/" + System.currentTimeMillis() + "_" + StrUtil.subAfter(waterMarkFilePath, "/", true);//String downloadPath = "D:\\hams\\r4.png";ImageIO.write(resize, "png", new File(downloadPath));return downloadPath;}}

实测可用。

java为word、excel、pdf、ppt、图片添加图片水印(文字水印同理)相关推荐

  1. java poi- 实现 word Excel pdf ppt 转 HTML

    所需要 jar poi-3.17.jar poi-examples-3.17.jar poi-excelant-3.17.jar poi-ooxml-3.17.jar poi-ooxml-schema ...

  2. office 文档 在线预览功能实现(word,excel,pdf,ppt等多种格式)——使用https://view.xdocin.com/view 提示文档过期——基础积累

    web实现office文档在线预览功能--基础积累 最近遇到一个需求,就是要实现多种文档链接的在线预览,最简单的方式就是通过window.open(url地址)的方式来实现. 但是如果要求是在一个弹窗 ...

  3. txt doc rtf html,JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例.docx

    JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例 JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例??2012-06-2 ...

  4. java 模板 word转pdf 可分页 带图片

    java 模板 word转pdf 可分页 带图片 之前写过一个简单的案例,但是在项目中完全不能满足客户的需求,所以重新用啦一种方式来写,采用了word转换pdf的方式,这种经过不断研究,满足了可分页, ...

  5. java操作word/excel/pdf等文件技术方案

    最近项目中遇到很多对word/excel/pdf等文件的操作,解决方案有好多,开源免费有:利用openoffice组件(需要安装openoffice软件),poi,itext等.也有收费的服务:asp ...

  6. java给word和pdf文档添加二维码

    背景 目前收到一个需求,需要给上传的word和pdf文档添加二维码,防止职工给领导签字时伪造合同,所以提出这个需求,上传的word和pdf添加二维码,然后使用我们的app扫码查看相关的信息. 解决方案 ...

  7. Java处理Word, Excel, PDF文档的4种开源系统的代码例子

    原文: http://blog.csdn.net/wzwfly/article/details/1645046   很多人用java进行文档操作时经常会遇到一个问题,就是如何获得word,excel, ...

  8. 给图片添加多条文字水印和图片水印

    原始图: 水印原始模板: 添加水印后的效果图 package com.sjaco.hy.api.test;import java.awt.image.BufferedImage; import jav ...

  9. 怎样边下载宝贝图片边给图片添加自己的文字水印或图片水印

    今天小编要介绍一个比较常用的技巧,就是如何在下载商品图片时,给商品主图添加一些个性的水印或文字?一起来看看具体的操作步骤吧. 首先复制要抓取的商品链接地址 小编使用的下载图片工具(载图助手),打开进入 ...

  10. java : word,excel,img,ppt各种文档转换pdf格式以流方式

    前提: 面对各种文件转换pdf格式,我下面写的都是一些方法,其中每个方法都以流的方式进行参数的传递. 通过猿友的帮忙,修改了部分jar包,解决excel转换pdf导致的水印问题~ 源码链接:https ...

最新文章

  1. java实体属性对应mysql和SQL Server 和Oracle 数据类型对应
  2. 一对一聊天ajax实现
  3. 命令行cmd跳转到其他地址
  4. linux命令:linux集群系列之一---LVS类型解析
  5. java set 接口_java笔记四:Set接口
  6. swoole深入学习 2. tcp Server和tcp Client
  7. HTML5文件API之FileReader
  8. linux 有道词典无法屏幕取词,有道词典怎么开启屏幕取词功能 有道词典开启屏幕取词功能方法...
  9. java基础练习实例_java基础练习题百度云.doc
  10. oracle的备份恢复命令,Oracle RMAN的备份与恢复命令详解
  11. Java从入门到放弃系列
  12. python网页教程_python网页教程
  13. 安全合规/法案--33--《APP违法违规收集使用个人信息自评估指南》原文及解读
  14. npm install没有node_文件,并且package.json文件缺失
  15. 如何把android studio中的项目发布到手机上(超详细版)
  16. 微信公众号模板消息推送(PHP)
  17. 申请微信小程序需要的材料
  18. 几个图像缩放算法的比较
  19. 【520表白】C语言开发《浪漫流星雨》表白程序,源码来了!
  20. 游标v_cur的%notfound

热门文章

  1. 将 s1 和 r1 上的启动配置文件上传到服务器进行备份,packettracer综合技能练习261...
  2. NFT Insider #43 Animoca Brands完成3.58亿美元融资,微软重金收购暴雪
  3. 离散数学10__第5章 关系与函数_关系的性质_自反对称传递
  4. 基于注解的Excel导出万能模板
  5. Python入门学习笔记第五章——if条件句~~~
  6. Qt焦点事件 setFocusPolicy
  7. VR头戴显示器对健康有害吗?会引发晕动症、视觉辐辏调节冲突
  8. 可怜的码农们该如何赚钱?
  9. 悖论对计算机科学影响,科学界最著名的几大悖论,你能解释吗?
  10. Google Earth上划定矢量范围并导出为KMZ文件