從oss下載pdf併爲pdf加水印並回傳

        需求:公司業務員從oss存儲中的下載pdf,爲了文件的安全需要給pdf文件加水印,而且由於我們使用的是前後端分離來開發的,所以需要把加了水印的pdf傳回給oss

首先需要的jar包爲iText-2.1.2u.jar、iTextAsian.jar。
package com.financial.core;

import java.awt.Color;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfGState;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;

public class TestWaterPrint {
public String getWaterPdf(String url,String salesmanOid) throws Exception{
//首先處理pdf的名字
String fileName = url.substring(url.lastIndexOf("/")+1,url.lastIndexOf("."));
//文件存儲路徑用配置文件配置,用於開發環境和正式環境-----至於爲什麼存這裏在博客的第一條異常信息中說明了
InputStream in = TestWaterPrint.class.getClassLoader().getResourceAsStream("fileulr.properties");
Properties prop = new Properties();
prop.load(in);
String filePath = (String) prop.get("url");
//爲了防止併發刪除給字字加個時間戳
File file = new File(filePath+fileName+(new Date()).getTime()+url.substring(url.indexOf(".")));
in.close();

//在水印中加上salesman的名字
SalesMan sman = SalesManDao.findSalesManByOid(salesmanOid);
       
//由於oss不支持流數據直接回傳
//所以在本地生成一個臨時文件
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
Calendar cal = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");    
//將pdf文件先加水印然後輸出
// setWatermark(bos,"E:/pdf源文件.pdf",format.format(cal.getTime()) + "  下載使用人:" + "測試user", 16);

PdfReader reader = new PdfReader(url);
PdfStamper stamper = new PdfStamper(reader, bos);
int total = reader.getNumberOfPages() + 1;
PdfContentByte content;
BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
PdfGState gs = new PdfGState();
for (int i = 1; i < total; i++) {
content = stamper.getOverContent(i);// 在內容上方加水印
// content = stamper.getUnderContent(i);//在內容下方加水印
gs.setFillOpacity(0.2f);          //設置透明度
// content.setGState(gs);
content.beginText();
content.setColorFill(Color.LIGHT_GRAY); //設置水印顏色
content.setFontAndSize(base, 50); //設置字體和大小
content.setTextMatrix(70, 200); //設置文本矩陣
//設置水印位置,後面三個數字,1:距離頁面左邊位置,2:距離頁面下面位置,3:水印傾斜度----350的位置大概在頁面中間
content.showTextAligned(Element.ALIGN_CENTER, "請注意保密!", 300, 350, 55);
content.setColorFill(Color.BLACK);
content.setFontAndSize(base, 8);
//10的位置在最下面,傾斜度爲0就是水平,這個相當於一個頁腳
content.showTextAligned(Element.ALIGN_CENTER, "下載時間:" + sman.getName() + "", 300, 10, 0);
content.endText();
}
stamper.close();      //一定要記得關流。如果要關reader和bos的流要注意關流順序

//到此加了水印的pdf就已經生成了,那就是上面的file
FileInputStream fis = new FileInputStream(file);
//爲了防止加水印的文件將原本正常文件覆蓋,所以文件名要區分開,爲了不佔用更多的oss存儲空間,每個sman給一個文件一個單獨的名字就夠了,防止重複上傳
String returnUrl = OssUtils.singleFileUpload(fis,fileName+sman.getName());  //這個utils是同事寫的,大家可以自己研究,aliyun.sdk裏面好像也有

                if(file.exist) {

                    file.delete();

                }

return returnUrl;
}
}
由於之前寫的代碼在公司沒有copy出來,所以在這裏只是按記憶寫的,有些不正確的地方就請大家見諒。

擴展學習http://www.open-open.com/lib/view/open1339726488115.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章