Commit d1f77a6f authored by suhuiguang's avatar suhuiguang

1.删除无用代码

parent 2daba24c
...@@ -33,7 +33,7 @@ public class PdfServiceImpl implements IPdfService { ...@@ -33,7 +33,7 @@ public class PdfServiceImpl implements IPdfService {
File imageFile = null, file = null; File imageFile = null, file = null;
try { try {
imageFile = File.createTempFile("temp", ".png"); imageFile = File.createTempFile("temp", ".png");
PdfUtils.generatorImageWithText(imageFile, 100, 50, text , Color.RED); PdfUtils.generatorImageWithText(imageFile, 100, 50, text, Color.RED);
file = PdfUtils.addImageToPdf(filePath, 100, 50, imageFile.getAbsolutePath()); file = PdfUtils.addImageToPdf(filePath, 100, 50, imageFile.getAbsolutePath());
String fileName = extractFileNameFromPath(inputPdfPath); String fileName = extractFileNameFromPath(inputPdfPath);
MultipartFile mockMultipartFile = new MockMultipartFile(fileName, fileName, ContentType.APPLICATION_OCTET_STREAM.toString(), new FileInputStream(file)); MultipartFile mockMultipartFile = new MockMultipartFile(fileName, fileName, ContentType.APPLICATION_OCTET_STREAM.toString(), new FileInputStream(file));
...@@ -45,10 +45,10 @@ public class PdfServiceImpl implements IPdfService { ...@@ -45,10 +45,10 @@ public class PdfServiceImpl implements IPdfService {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
if(imageFile != null){ if (imageFile != null) {
imageFile.deleteOnExit(); imageFile.deleteOnExit();
} }
if(file != null){ if (file != null) {
file.deleteOnExit(); file.deleteOnExit();
} }
} }
...@@ -59,10 +59,8 @@ public class PdfServiceImpl implements IPdfService { ...@@ -59,10 +59,8 @@ public class PdfServiceImpl implements IPdfService {
public String signToPdfWaterPrint(String inputPdfPath, String text) { public String signToPdfWaterPrint(String inputPdfPath, String text) {
String newFilePath = ""; String newFilePath = "";
String filePath = fileServerUrl + inputPdfPath; String filePath = fileServerUrl + inputPdfPath;
File imageFile = null, file = null; File file = null;
try { try {
imageFile = File.createTempFile("temp", ".png");
PdfUtils.generatorImageWithText(imageFile, 200, 100, text , Color.RED);
file = PdfUtils.addFullPageWatermark(filePath, "已作废"); file = PdfUtils.addFullPageWatermark(filePath, "已作废");
String fileName = extractFileNameFromPath(inputPdfPath); String fileName = extractFileNameFromPath(inputPdfPath);
MultipartFile mockMultipartFile = new MockMultipartFile(fileName, fileName, ContentType.APPLICATION_OCTET_STREAM.toString(), new FileInputStream(file)); MultipartFile mockMultipartFile = new MockMultipartFile(fileName, fileName, ContentType.APPLICATION_OCTET_STREAM.toString(), new FileInputStream(file));
...@@ -74,10 +72,7 @@ public class PdfServiceImpl implements IPdfService { ...@@ -74,10 +72,7 @@ public class PdfServiceImpl implements IPdfService {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
if(imageFile != null){ if (file != null) {
imageFile.deleteOnExit();
}
if(file != null){
file.deleteOnExit(); file.deleteOnExit();
} }
} }
......
...@@ -93,13 +93,10 @@ public class PdfUtils { ...@@ -93,13 +93,10 @@ public class PdfUtils {
outputPdfFile = File.createTempFile("output", ".pdf"); outputPdfFile = File.createTempFile("output", ".pdf");
reader = new PdfReader(src); reader = new PdfReader(src);
stamper = new PdfStamper(reader, new FileOutputStream(outputPdfFile)); stamper = new PdfStamper(reader, new FileOutputStream(outputPdfFile));
stamper.setFormFlattening(true);
PdfGState gs = new PdfGState(); PdfGState gs = new PdfGState();
// 设置透明度为0.1 // 设置透明度为0.1
gs.setFillOpacity(0.2f); gs.setFillOpacity(0.2f);
int n = reader.getNumberOfPages(); int n = reader.getNumberOfPages();
// 创建一个BaseFont实例,用于水印文本 // 创建一个BaseFont实例,用于水印文本
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED); BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
// 设置水印文本大小 // 设置水印文本大小
...@@ -110,8 +107,7 @@ public class PdfUtils { ...@@ -110,8 +107,7 @@ public class PdfUtils {
float x = 0; float x = 0;
// 遍历PDF的每一页 // 遍历PDF的每一页
for (int i = 1; i <= n; i++) { for (int i = 1; i <= n; i++) {
PdfContentByte over = stamper.getUnderContent(i); PdfContentByte over = stamper.getOverContent(i);
// 获取页面尺寸 // 获取页面尺寸
Rectangle pageSize = reader.getPageSizeWithRotation(i); Rectangle pageSize = reader.getPageSizeWithRotation(i);
float pageWidth = pageSize.getWidth(); float pageWidth = pageSize.getWidth();
...@@ -119,10 +115,8 @@ public class PdfUtils { ...@@ -119,10 +115,8 @@ public class PdfUtils {
// 从页面底部开始 // 从页面底部开始
float y = pageHeight; float y = pageHeight;
// 计算文本宽度 // 计算文本宽度
float textWidth = bf.getWidthPoint(watermarkText, fontSize); float textWidth = bf.getWidthPoint(watermarkText, fontSize);
// 添加水印文本到页面 // 添加水印文本到页面
while (y > 0) { while (y > 0) {
// 文本从页面左侧开始 // 文本从页面左侧开始
...@@ -133,6 +127,8 @@ public class PdfUtils { ...@@ -133,6 +127,8 @@ public class PdfUtils {
//水印颜色 //水印颜色
over.setColorFill(BaseColor.BLACK); over.setColorFill(BaseColor.BLACK);
over.setGState(gs); over.setGState(gs);
// 设置文本渲染模式为填充,使文本不可选择
over.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL);
over.setTextMatrix(x, y); over.setTextMatrix(x, y);
over.showTextAligned(Element.ALIGN_CENTER, watermarkText, x, y, 45); over.showTextAligned(Element.ALIGN_CENTER, watermarkText, x, y, 45);
over.endText(); over.endText();
...@@ -156,4 +152,5 @@ public class PdfUtils { ...@@ -156,4 +152,5 @@ public class PdfUtils {
return outputPdfFile; return outputPdfFile;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment