从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

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