pdf预览水印问题
This commit is contained in:
@@ -70,6 +70,11 @@
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-openfeign-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.itextpdf</groupId>
|
||||
<artifactId>itext-asian</artifactId>
|
||||
<version>5.2.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.lowagie</groupId>
|
||||
<artifactId>itext</artifactId>
|
||||
|
||||
+116
-13
@@ -1,26 +1,24 @@
|
||||
package com.jero.modules.system.util;
|
||||
|
||||
import com.itextpdf.text.BaseColor;
|
||||
import com.itextpdf.text.DocumentException;
|
||||
import com.itextpdf.text.Element;
|
||||
import com.itextpdf.text.pdf.*;
|
||||
import com.spire.pdf.PdfDocument;
|
||||
import com.spire.pdf.PdfPageBase;
|
||||
import com.spire.pdf.graphics.PdfBrushes;
|
||||
import com.spire.pdf.graphics.PdfFont;
|
||||
import com.spire.pdf.graphics.PdfFontFamily;
|
||||
import com.spire.pdf.graphics.PdfStringFormat;
|
||||
import com.spire.pdf.graphics.PdfTextAlignment;
|
||||
import com.spire.pdf.graphics.PdfTilingBrush;
|
||||
import com.spire.pdf.graphics.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.geom.Dimension2D;
|
||||
import java.awt.geom.Point2D;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.*;
|
||||
|
||||
@Slf4j
|
||||
public class PDFUtils {
|
||||
public static void main(String[] args) throws IOException {
|
||||
File file = new File("E:\\DeskTop\\动态资料\\加入IEEE会员 - 副本 - 副本.pdf");
|
||||
@@ -84,7 +82,7 @@ public class PDFUtils {
|
||||
page.getCanvas().drawRectangle(brush, loRect);
|
||||
}
|
||||
|
||||
public static File PDFWatermark(InputStream in,String uploadpath, String fileName,String waterMark) throws FileNotFoundException {
|
||||
public static File PDFWatermarkInvalid(InputStream in,String uploadpath, String fileName,String waterMark) throws FileNotFoundException {
|
||||
//创建PdfDocument类的对象
|
||||
PdfDocument pdf = new PdfDocument();
|
||||
//加载测试文档
|
||||
@@ -203,4 +201,109 @@ public class PDFUtils {
|
||||
// }
|
||||
// return buffer;
|
||||
// }
|
||||
|
||||
public static File PDFWatermark(InputStream in,String uploadpath, String fileName,String waterMark) {
|
||||
|
||||
String newFilePath = "";
|
||||
// 待加水印的文件
|
||||
PdfReader reader = null;
|
||||
PdfStamper stamper = null;
|
||||
FileOutputStream os = null;
|
||||
|
||||
try {
|
||||
byte[] byes = IOUtils.toByteArray(in);
|
||||
|
||||
//生成临时文件 , 读取完删除
|
||||
String path = uploadpath + File.separator + fileName;
|
||||
newFilePath = path.replace(fileName, "transformation" + fileName);
|
||||
reader = new PdfReader(byes);
|
||||
|
||||
// 加完水印的文件
|
||||
os = new FileOutputStream(newFilePath);
|
||||
stamper = new PdfStamper(reader, os);
|
||||
|
||||
int total = reader.getNumberOfPages() + 1;
|
||||
BaseFont basefont = BaseFont.createFont("/static/times.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
|
||||
// 方法二:这里的字体设置比较关键,这个设置是支持中文的写法
|
||||
// BaseFont basefont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 使用系统字体
|
||||
|
||||
PdfContentByte under;
|
||||
com.itextpdf.text.Rectangle pageRect = null;
|
||||
|
||||
//水印倾斜角度(0-360)
|
||||
int angle = 45;
|
||||
//数值越大每页竖向水印越少
|
||||
int heightDensity = 10;
|
||||
//数值越大每页横向水印越少
|
||||
int widthDensity = 1;
|
||||
int textH = 0;
|
||||
int textW = 0;
|
||||
JLabel label = new JLabel();
|
||||
FontMetrics metrics;
|
||||
label.setText(waterMark);
|
||||
metrics = label.getFontMetrics(label.getFont());
|
||||
//字符串的高, 只和字体有关
|
||||
textH = metrics.getHeight();
|
||||
//字符串的宽
|
||||
textW = metrics.stringWidth(label.getText());
|
||||
|
||||
// 循环对每页插入水印
|
||||
for (int i = 1; i < total; i++) {
|
||||
pageRect = stamper.getReader().getPageSizeWithRotation(i);
|
||||
|
||||
// 获得PDF最顶层
|
||||
under = stamper.getOverContent(i);
|
||||
under.saveState();
|
||||
// set Transparency
|
||||
PdfGState gs = new PdfGState();
|
||||
// 设置透明度
|
||||
gs.setFillOpacity(0.5f);
|
||||
under.setGState(gs);
|
||||
under.restoreState();
|
||||
under.beginText();
|
||||
under.setFontAndSize(basefont, pageRect.getHeight() / 100);
|
||||
under.setColorFill(BaseColor.PINK);
|
||||
|
||||
// 水印文字成45度角倾斜
|
||||
for (int height = textH; height < pageRect.getHeight() * 3; height = height + textH * heightDensity) {
|
||||
for (int width = textW; width < pageRect.getWidth() * 1.5 + textW; width = width + textW * widthDensity) {
|
||||
// rotation:倾斜角度
|
||||
under.showTextAligned(Element.ALIGN_CENTER, waterMark, width - textW, height - textH, angle);
|
||||
}
|
||||
}
|
||||
|
||||
// 添加水印文字
|
||||
under.endText();
|
||||
under.setLineWidth(1f);
|
||||
under.stroke();
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
log.error(e.getMessage());
|
||||
|
||||
} catch (DocumentException e) {
|
||||
e.printStackTrace();
|
||||
log.error(e.getMessage());
|
||||
|
||||
} finally {
|
||||
try {
|
||||
stamper.close();
|
||||
if (os != null) {
|
||||
os.close();
|
||||
}
|
||||
if (reader != null) {
|
||||
reader.close();
|
||||
}
|
||||
} catch (DocumentException e) {
|
||||
e.printStackTrace();
|
||||
log.error(e.getMessage());
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
return new File(newFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user