先上效果图 由于透明度设置的比较低,可能看不太清右上角的是文字和图片的水印,图片打码了。
该实现中有word转pdf的方法,如有需要请移步另一个word转pdf的帖子,小白一个写的比较冗余哈。
pom
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
业务逻辑
@Override
public void loadPdf(HttpServletRequest request, HttpServletResponse response, PdfDTO pdfDTO) throws Exception {
String realPath = request.getSession().getServletContext().getRealPath(File.separator);
long docTime = System.currentTimeMillis();
if (pdfDTO.getStrUrl().contains(".doc") || pdfDTO.getStrUrl().contains(".docx")) {
logger.info("realPath路径为:" + realPath);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String fileAdd = sdf.format(new Date());
String inputPath = realPath + File.separator + fileAdd + File.separator;
logger.info("inputPath路径为:" + inputPath);
File file = new File(inputPath);
if (!file.exists() && !file.isDirectory()) {
file.mkdir();
}
long newPdfTime = System.currentTimeMillis();
String fileName = docTime + ".doc";
String newPdf = newPdfTime + ".pdf";
String rePath = inputPath + File.separator + File.separator + fileName;
String newPdfPath = inputPath + File.separator + File.separator + newPdf;
downloadFile(pdfDTO.getStrUrl(), rePath);
logger.info("保存的doc路径:" + realPath);
long l = System.currentTimeMillis();
PdfUtil.doc2pdf(rePath, newPdfPath);
logger.info("doc转pdf生成的新pdf路径:" + newPdfPath);
long l1 = System.currentTimeMillis();
logger.info("生成pdf用时为:" + (l1 - l));
Date date = new Date();
SimpleDateFormat sdff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date1 = sdff.format(date);
String waterPdf = System.currentTimeMillis() + ".pdf";
String waterPdfPath = inputPath + File.separator + File.separator + waterPdf;
logger.info("打完水印的路径为:" + waterPdfPath);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(waterPdfPath)));
waterMark(bos, newPdfPath, pdfDTO.getType());
downPdf(request, response, waterPdfPath);
deleteDir(inputPath);
return;
}
long l = System.currentTimeMillis();
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date1 = simpleDateFormat.format(date);
logger.info("realPath路径为:" + realPath);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String fileAdd = sdf.format(new Date());
String inputPath = realPath + File.separator + fileAdd + File.separator;
logger.info("inputPath路径为:" + inputPath);
File file = new File(inputPath);
if (!file.exists() && !file.isDirectory()) {
file.mkdir();
}
String fileName = l + ".pdf";
logger.info("fileName文件名:" + fileName);
String rePath = inputPath + File.separator + File.separator + fileName;
logger.info("rePath保存路径以及文件名:" + rePath);
waterMark(new URL(pdfDTO.getStrUrl()), rePath, pdfDTO.getType());
downPdf(request, response, rePath);
deletePdf(inputPath, fileName);
}
主要实现
public static void waterMark(BufferedOutputStream bos, String input, Integer type) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String format = sdf.format(date)