java文件下载并添加水印_Java下载文件加文字水印(Excel、PDF、图片)
一、導(dǎo)出Excel加文字水印
方法:通過實(shí)現(xiàn)準(zhǔn)備一個帶有水印的Excel文件,然后將數(shù)據(jù)覆蓋該文件然后輸出。
比如我準(zhǔn)備了如下的一個帶水印的模板,我起名為water.xlsx(網(wǎng)上有如何給Excel加水印)
我是在原有導(dǎo)出的方法上進(jìn)行改造,原方法使用的正是POI。
1、改造創(chuàng)建工作簿的方法
this.wb指的就是Workbook類。
/*** 創(chuàng)建一個工作簿*/
public void createWorkbook() throwsException {//獲取有水印的excel模板文件
File finalXlsxFile = new File("src/main/resources/static/water.xlsx");//獲取excel文件流
FileInputStream inputStream = newFileInputStream(finalXlsxFile);this.wb = newXSSFWorkbook(inputStream);
}
2、改造創(chuàng)建工作表的方法
this.sheet指的就是Sheet類。
//原來創(chuàng)建空的Sheet為方法://this.sheet = wb.createSheet();//現(xiàn)在改為:
this.sheet = wb.getSheetAt(0);
通過以上改造就可導(dǎo)出帶有水印的Excel。
二、下載PDF加入文字水印
方法:itext提供了添加水印的方法。
1、首先加入依賴
com.itextpdf
itextpdf
5.2.0
com.itextpdf
itext-asian
5.2.0
2、加入以下代碼
/*** 設(shè)置文字水印
*
*@paramos 輸出流
*@paramsrcFile 源文件
*@paramwaterMarkName 水印文字
*@throwsDocumentException
*@throwsIOException*/
public static void setWordMark(OutputStream os, String srcFile, String waterMarkName) throwsDocumentException, IOException {//如果你想直接輸出到某個路徑,將os參數(shù)改為descFile(具體輸出路徑)//BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(descFile)));
PdfReader reader= newPdfReader(srcFile);
PdfStamper stamper= newPdfStamper(reader, os);int total = reader.getNumberOfPages() + 1;
PdfContentByte content;
BaseFont base= BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
PdfGState gs= newPdfGState();for (int i = 1; i < total; i++) {
content= stamper.getOverContent(i); //在內(nèi)容上方加水印//content = stamper.getUnderContent(i);//在內(nèi)容下方加水印
gs.setFillOpacity(0.2f); //透明度
content.setGState(gs);
content.beginText();
content.setColorFill(BaseColor.BLACK);
content.setFontAndSize(base,50);
content.setTextMatrix(70, 200);
content.showTextAligned(Element.ALIGN_CENTER, waterMarkName,300, 350, 55);
content.endText();
}
stamper.close();
}
3、直接使用
你下載請求Controller的時候,直接使用,例如:
這里的downloadPath指的是要下載的文件路徑,"奧里給"為水印文字。
PDFUtils.setWordMark(response.getOutputStream(), downloadPath, "奧里給");
三、下載圖片加文字水印
方法:使用Graphics2D在圖片上直接繪畫出水印。
1、直接復(fù)制以下代碼
注意我這里也是用輸出流作為參數(shù)。
/*** 圖片水印處理*/
public classImageUtils {//水印透明度
private static float alpha = 0.5f;//水印橫向位置
private static int positionWidth = 150;//水印縱向位置
private static int positionHeight = 300;//水印文字字體
private static Font font = new Font("宋體", Font.BOLD, 72);//水印文字顏色
private static Color color =Color.red;/*** 給圖片添加水印文字、可設(shè)置水印文字的旋轉(zhuǎn)角度
*
*@paramlogoText 水印文字
*@paramsrcImgPath 源文件路徑
*@paramos 輸出流
*@paramdegree 設(shè)置角度*/
public static voidmarkImageByText(String logoText, String srcImgPath,
OutputStream os, Integer degree) {
InputStream is= null;try{//1、源圖片
Image srcImg = ImageIO.read(newFile(srcImgPath));
BufferedImage buffImg= new BufferedImage(srcImg.getWidth(null),
srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);//2、得到畫筆對象
Graphics2D g =buffImg.createGraphics();//3、設(shè)置對線段的鋸齒狀邊緣處理
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(
srcImg.getScaledInstance(srcImg.getWidth(null),
srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0,null);//4、設(shè)置水印旋轉(zhuǎn)
if (null !=degree) {
g.rotate(Math.toRadians(degree),
(double) buffImg.getWidth() / 2,
(double) buffImg.getHeight() / 2);
}//5、設(shè)置水印文字顏色
g.setColor(color);//6、設(shè)置水印文字Font
g.setFont(font);//7、設(shè)置水印文字透明度
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
alpha));//8、第一參數(shù)->設(shè)置的內(nèi)容,后面兩個參數(shù)->文字在圖片上的坐標(biāo)位置(x,y)
g.drawString(logoText, positionWidth, positionHeight);//9、釋放資源
g.dispose();//10、生成圖片
ImageIO.write(buffImg, "JPG", os);
System.out.println("圖片完成添加水印文字");
}catch(Exception e) {
e.printStackTrace();
}finally{try{if (null !=is)
is.close();
}catch(Exception e) {
e.printStackTrace();
}try{if (null !=os)
os.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
2、直接調(diào)用
“奧里給”為水印文字,downloadPath為 源文件地址,最后一個參數(shù)應(yīng)該是設(shè)置角度的,我這里設(shè)置為null。
ImageUtils.markImageByText("奧里給", downloadPath, response.getOutputStream(), null);
如果有其他需求的請參考我放的鏈接,感謝原作者。
總結(jié)
以上是生活随笔為你收集整理的java文件下载并添加水印_Java下载文件加文字水印(Excel、PDF、图片)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql数据库在什么上运行_mysql
- 下一篇: java开发怎么打补丁_[Java教程]