预览添加水印
This commit is contained in:
+27
-9
@@ -3,16 +3,19 @@ package com.jero.modules.system.controller;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.jero.common.api.vo.Result;
|
||||
import com.jero.common.constant.CommonConstant;
|
||||
import com.jero.common.exception.JeroBootException;
|
||||
import com.jero.common.util.*;
|
||||
import com.jero.common.system.api.ISysBaseAPI;
|
||||
import com.jero.common.util.RestUtil;
|
||||
import com.jero.common.util.TokenUtils;
|
||||
import com.jero.common.util.oConvertUtils;
|
||||
import com.jero.modules.oss.entity.OSSFile;
|
||||
import com.jero.modules.oss.service.IOSSFileService;
|
||||
import com.jero.modules.system.util.PDFUtils;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import com.jero.common.api.vo.Result;
|
||||
import com.jero.common.constant.CommonConstant;
|
||||
import com.jero.common.system.api.ISysBaseAPI;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
@@ -21,8 +24,12 @@ import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.server.ServletServerHttpRequest;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
@@ -31,7 +38,12 @@ import org.springframework.web.servlet.ModelAndView;
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.List;
|
||||
|
||||
@@ -378,7 +390,7 @@ public class CommonController {
|
||||
|
||||
}
|
||||
/**
|
||||
* 文件预览
|
||||
* 文件预览PDF
|
||||
*
|
||||
* @param id 传入文件id
|
||||
* @param request
|
||||
@@ -400,13 +412,19 @@ public class CommonController {
|
||||
//本地下载
|
||||
String filePath = ossFile.getUrl();
|
||||
File file = new File(filePath);
|
||||
//水印内容
|
||||
String waterContent = "water mark";//临时定义(水印信息通过传过来)
|
||||
File newFile = PDFUtils.PDFWatermark(file,waterContent);
|
||||
String path = newFile.getPath();
|
||||
// String substring = path.substring(filePath.length(), path.length());
|
||||
|
||||
if(!file.exists()){
|
||||
response.setStatus(404);
|
||||
throw new RuntimeException("文件不存在..");
|
||||
}
|
||||
// 文件名称
|
||||
fileName = file.getName();
|
||||
inputStream = new BufferedInputStream(new FileInputStream(filePath));
|
||||
inputStream = new BufferedInputStream(new FileInputStream(path));
|
||||
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes("UTF-8"),"iso-8859-1"));
|
||||
response.setContentType("application/force-download");// 设置强制下载不打开
|
||||
outputStream = response.getOutputStream();
|
||||
|
||||
+170
@@ -0,0 +1,170 @@
|
||||
package com.jero.modules.system.util;
|
||||
|
||||
import com.spire.pdf.*;
|
||||
import com.spire.pdf.graphics.*;
|
||||
import java.awt.*;
|
||||
import java.awt.geom.*;
|
||||
import java.io.*;
|
||||
|
||||
public class PDFUtils {
|
||||
public static void main(String[] args) throws IOException {
|
||||
File file = new File("E:\\DeskTop\\动态资料\\加入IEEE会员 - 副本 - 副本.pdf");
|
||||
InputStream in = new FileInputStream(file) ;
|
||||
// byte[] byteArray=File2byte(new File("f:ceshi/1.pdf"));
|
||||
//创建PdfDocument类的对象
|
||||
PdfDocument pdf = new PdfDocument();
|
||||
//加载测试文档
|
||||
pdf.loadFromStream(in);
|
||||
try {
|
||||
pdf.getPages().add();
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
//获取测试文档中的第一页
|
||||
for(int i=0; i<pdf.getPages().getCount();i++){
|
||||
|
||||
PdfPageBase page = pdf.getPages().get(i);
|
||||
//调用insertWatermark()方法添加文本水印
|
||||
insertWatermark(page, "TOP SECRET");
|
||||
}
|
||||
//
|
||||
|
||||
pdf.getPages().remove(pdf.getPages().get(pdf.getPages().getCount()-1));
|
||||
String a = "E:\\DeskTop\\动态资料\\1.pdf";
|
||||
//保存文档
|
||||
pdf.saveToFile(a);
|
||||
File file1 = new File(a);
|
||||
File file2 = new File("E:\\DeskTop\\动态资料\\2.pdf");
|
||||
InputStream in1 = new FileInputStream(file1);
|
||||
|
||||
OutputStream out2 = new FileOutputStream(file2);
|
||||
byte[] buffer = new byte[4096];
|
||||
int n = 0;
|
||||
while (-1 != (n = in1.read(buffer))) {
|
||||
out2.write(buffer, 0, n);
|
||||
}
|
||||
file1.delete();
|
||||
|
||||
// ByteArrayOutputStream[] out = pdf.saveToStream(0,0,FileFormat.PDF);
|
||||
// out .toByteArray();
|
||||
|
||||
}
|
||||
|
||||
static void insertWatermark(PdfPageBase page, String watermark) {
|
||||
Dimension2D dimension2D = new Dimension();
|
||||
dimension2D.setSize(page.getCanvas().getClientSize().getWidth() / 3, page.getCanvas().getClientSize().getHeight() / 3);
|
||||
PdfTilingBrush brush = new PdfTilingBrush(dimension2D);
|
||||
brush.getGraphics().setTransparency(0.4F);
|
||||
brush.getGraphics().save();
|
||||
brush.getGraphics().translateTransform((float) brush.getSize().getWidth() / 5, (float) brush.getSize().getHeight() / 5);
|
||||
brush.getGraphics().rotateTransform(-45);
|
||||
brush.getGraphics().drawString(watermark, new PdfFont(PdfFontFamily.Helvetica, 15),
|
||||
PdfBrushes.getViolet(), 0 , 0 , new PdfStringFormat(PdfTextAlignment.Center));
|
||||
brush.getGraphics().restore();
|
||||
brush.getGraphics().setTransparency(1);
|
||||
Rectangle2D loRect = new Rectangle2D.Float();
|
||||
loRect.setFrame(new Point2D.Float(0, 0), page.getCanvas().getClientSize());
|
||||
page.getCanvas().drawRectangle(brush, loRect);
|
||||
}
|
||||
|
||||
public static File PDFWatermark(File file,String waterMark) throws FileNotFoundException {
|
||||
InputStream in = new FileInputStream(file) ;
|
||||
// byte[] byteArray=File2byte(new File("f:ceshi/1.pdf"));
|
||||
//创建PdfDocument类的对象
|
||||
PdfDocument pdf = new PdfDocument();
|
||||
//加载测试文档
|
||||
pdf.loadFromStream(in);
|
||||
// pdf.loadFromFile(file.getPath());
|
||||
//为了去掉在第一页多余的水印,加一页,后边删除
|
||||
pdf.getPages().add();
|
||||
|
||||
//获取测试文档中的第一页
|
||||
for(int i=0; i<pdf.getPages().getCount();i++){
|
||||
|
||||
PdfPageBase page = pdf.getPages().get(i);
|
||||
//调用insertWatermark()方法添加文本水印
|
||||
insertWatermark(page, waterMark);
|
||||
}
|
||||
// 删除加的第一页
|
||||
|
||||
pdf.getPages().remove(pdf.getPages().get(pdf.getPages().getCount()-1));
|
||||
String newFilePath = file.getPath().replace(file.getName(),"transformation"+file.getName());
|
||||
//保存文档
|
||||
pdf.saveToFile(newFilePath);
|
||||
// File newFile =
|
||||
// File file2 = new File("f:ceshi/4.pdf");
|
||||
// InputStream in1 = new FileInputStream(file1);
|
||||
//
|
||||
// OutputStream out2 = new FileOutputStream(file2);
|
||||
// byte[] buffer = new byte[4096];
|
||||
// int n = 0;
|
||||
// while (-1 != (n = in1.read(buffer))) {
|
||||
// out2.write(buffer, 0, n);
|
||||
// }
|
||||
// file1.delete();
|
||||
return new File(newFilePath);
|
||||
}
|
||||
public static File PDFWatermark(String newFilePath,InputStream in,String waterMark) throws FileNotFoundException {
|
||||
// InputStream in = new FileInputStream(file) ;
|
||||
// byte[] byteArray=File2byte(new File("f:ceshi/1.pdf"));
|
||||
//创建PdfDocument类的对象
|
||||
PdfDocument pdf = new PdfDocument();
|
||||
//加载测试文档
|
||||
pdf.loadFromStream(in);
|
||||
// pdf.loadFromFile(file.getPath());
|
||||
//为了去掉在第一页多余的水印,加一页,后边删除
|
||||
try {
|
||||
pdf.getPages().add();
|
||||
}catch (Exception e){
|
||||
//暂时不做处理
|
||||
}
|
||||
|
||||
//获取测试文档中的第一页
|
||||
for(int i=0; i<pdf.getPages().getCount();i++){
|
||||
|
||||
PdfPageBase page = pdf.getPages().get(i);
|
||||
//调用insertWatermark()方法添加文本水印
|
||||
insertWatermark(page, waterMark);
|
||||
}
|
||||
// 删除加的第一页
|
||||
|
||||
pdf.getPages().remove(pdf.getPages().get(pdf.getPages().getCount()-1));
|
||||
// String newFilePath = file.getPath().replace(file.getName(),"/transformation"+file.getName());
|
||||
//保存文档
|
||||
pdf.saveToFile(newFilePath);
|
||||
// File newFile =
|
||||
// File file2 = new File("f:ceshi/4.pdf");
|
||||
// InputStream in1 = new FileInputStream(file1);
|
||||
//
|
||||
// OutputStream out2 = new FileOutputStream(file2);
|
||||
// byte[] buffer = new byte[4096];
|
||||
// int n = 0;
|
||||
// while (-1 != (n = in1.read(buffer))) {
|
||||
// out2.write(buffer, 0, n);
|
||||
// }
|
||||
// file1.delete();
|
||||
return new File(newFilePath);
|
||||
}
|
||||
// public static byte[] File2byte(File filePath) {
|
||||
// byte[] buffer = null;
|
||||
// try {
|
||||
// FileInputStream fis = new FileInputStream(filePath);
|
||||
// ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
// byte[] b = new byte[1024];
|
||||
// int n;
|
||||
// while ((n = fis.read(b)) != -1) {
|
||||
// bos.write(b, 0, n);
|
||||
// }
|
||||
// fis.close();
|
||||
// bos.close();
|
||||
// buffer = bos.toByteArray();
|
||||
// } catch (FileNotFoundException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (IOException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// return buffer;
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user