Commit d1f77a6f authored by suhuiguang's avatar suhuiguang

1.删除无用代码

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