增加水印修改

master
hcy 2 years ago
parent ea2795cc44
commit 7dd88fc5c0

@ -257,7 +257,7 @@ public class BatchExportServiceImpl implements BatchExportService {
bis = new BufferedInputStream(new ByteArrayInputStream(file1)); bis = new BufferedInputStream(new ByteArrayInputStream(file1));
//输出 //输出
int len = 0; int len = 0;
byte[] buf = new byte[1024 * 1024]; byte[] buf = new byte[100 * 1024];
while ((len = bis.read(buf, 0, buf.length)) != -1) { while ((len = bis.read(buf, 0, buf.length)) != -1) {
bos.write(buf, 0, len); bos.write(buf, 0, len);
} }
@ -383,22 +383,22 @@ public class BatchExportServiceImpl implements BatchExportService {
e.printStackTrace(); e.printStackTrace();
return false; return false;
} finally { } finally {
try { //try {
if (writer != null) { if (writer != null) {
writer.close(); writer.close();
} }
if (fileOutputStream != null) { /*if (fileOutputStream != null) {
fileOutputStream.close(); fileOutputStream.close();
} }
if (bos != null) { if (bos != null) {
bos.flush(); bos.flush();
bos.close(); bos.close();
} }*/
} catch (IOException e) { /*} catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }*/
} }
return true; return true;
} }

@ -468,8 +468,7 @@ public class img2PdfUtil {
PdfStamper stamper = null; PdfStamper stamper = null;
try { try {
reader = new PdfReader(bos.toByteArray()); reader = new PdfReader(bos.toByteArray());
// PDF总页数 bos.reset(); // 重置 bos以便 PdfStamper 可以写入
int total = reader.getNumberOfPages();
// 加完水印的文件 // 加完水印的文件
if (null != response) { if (null != response) {
stamper = new PdfStamper(reader, response.getOutputStream()); stamper = new PdfStamper(reader, response.getOutputStream());
@ -482,7 +481,9 @@ public class img2PdfUtil {
// 循环对每页插入水印 // 循环对每页插入水印
PdfContentByte content; PdfContentByte content;
PdfGState gs = new PdfGState(); PdfGState gs = new PdfGState();
for (int i = 1; i < total; i++) { // PDF总页数
int total = reader.getNumberOfPages();
for (int i = 1; i <= total; i++) {
//upOrUnder = 1为在文本之上 //upOrUnder = 1为在文本之上
if (upOrUnder == 1) { if (upOrUnder == 1) {
// 水印在之前文本之上 // 水印在之前文本之上
@ -524,17 +525,17 @@ public class img2PdfUtil {
// 底部水印 // 底部水印
content.endText(); content.endText();
//第二水印 //第二水印
//content.beginText(); content.beginText();
//16进制颜色转color //16进制颜色转color
//content.setColorFill(color); content.setColorFill(color);
// 设置字体及字号 // 设置字体及字号
//content.setFontAndSize(font, textSize); content.setFontAndSize(font, textSize);
// 设置起始位置 // 设置起始位置
//content.setTextMatrix(textX, textY); content.setTextMatrix(textX, textY);
// 中间水印 // 中间水印
//content.showTextAligned(Element.ALIGN_LEFT, text, textX, textY, textRotation); content.showTextAligned(Element.ALIGN_LEFT, text, textX, textY, textRotation);
// 底部水印 // 底部水印
//content.endText(); content.endText();
} }
} }
} catch (IOException | DocumentException e) { } catch (IOException | DocumentException e) {

Loading…
Cancel
Save