java實現(xiàn)pdf文件添加水印,下載到瀏覽器
添加itextpdf依賴
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.8</version>
</dependency>
文件下載到瀏覽器和指定路徑
根據(jù)需求,不需要指定路徑可以刪除對應(yīng)的輸出流
public void addPDFImageWaterMark(MultipartFile srcFile, MultipartFile imagePath, HttpServletResponse response) {
String fileName = "test.pdf";
PdfReader reader = null;
PdfStamper stamper = null;
FileInputStream fileInputStream = null;
//這個輸出流要放入PdfStamper構(gòu)造方法中,這里也會下載一個文件,不需要的話可以在finally中刪除
FileOutputStream fileOutputStream = null;
//下載到你指定的路徑,try里面new的路徑,不需要下載到指定路徑可以刪除
FileOutputStream fos = null;
// 設(shè)置響應(yīng)頭,指定內(nèi)容類型和文件名,準(zhǔn)備下載到瀏覽器,下載到指定位置,不需要下載到瀏覽器可以刪除這個輸出流
ServletOutputStream outputStream = null;
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=" + srcFile.getOriginalFilename());
try {
outputStream = response.getOutputStream();
//如果是路徑的話可以使用另一個構(gòu)造方法reader = new PdfReader(srcPath);
reader = new PdfReader(srcFile.getBytes());
fileOutputStream = new FileOutputStream(fileName);
stamper = new PdfStamper(reader, fileOutputStream);
//加載圖片
//如果是路徑的話可以使用另一個構(gòu)造方法Image image =Image.getInstance(imagePath);
Image image = Image.getInstance(imagePath.getBytes());
//將圖片控制大小,適配這個大小
image.scaleToFit(200, 100);
PdfGState gs = new PdfGState();
//gs.setFillOpacity(0.2f);//圖片水印透明度
//gs.setStrokeOpacity(0.4f);//設(shè)置筆觸字體不透明度
PdfContentByte content = null;
int total = reader.getNumberOfPages();//pdf文件頁數(shù)
for (int i = 0; i < total; i++) {
float x = reader.getPageSize(i + 1).getWidth();//頁寬度
float y = reader.getPageSize(i + 1).getHeight();//頁高度
content = stamper.getOverContent(i + 1);
content.setGState(gs);
content.beginText();//開始寫入
//每頁7行,一行3個
for (int j=0; j<3; j++) {
for (int k=0; k<7; k++) {
//setAbsolutePosition 方法的參數(shù)(輸出水印X軸位置,Y軸位置)
image.setAbsolutePosition(x/3*j-30, y/7*k-20);
content.addImage(image);
}
}
content.endText();//結(jié)束寫入
}
//要先關(guān)閉流才能將生成的文件寫到指定地方?。?!
stamper.close();
reader.close();
//指定這個文件(這里我用的相對路徑)
fileInputStream = new FileInputStream(fileName);
//創(chuàng)建輸出流,下載到指定路徑
fos = new FileOutputStream("test1.pdf");
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
// 瀏覽器下載
outputStream.write(buffer, 0, bytesRead);
//下載到指定路徑
fos.write(buffer, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
//關(guān)閉流
if (stamper != null) {
stamper.close();
}
if (reader != null) {
reader.close();
}
if (fos != null) {
fos.close();
}
if (fileInputStream != null) {
fileInputStream.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
效果如下:代碼中的相對路徑在src平級目錄下,test.pdf是PdfStamper里面fileOutputStream生成的,test1.pdf是fos生成的
瀏覽器下載的如下:
生成的pdf內(nèi)容如下(紅框里面是pdf原來的內(nèi)容,可以自己調(diào)整代碼中注釋掉的設(shè)置水印透明度來調(diào)整)
文章來源:http://www.zghlxwxcb.cn/news/detail-743022.html
提供的前端代碼自行測試文章來源地址http://www.zghlxwxcb.cn/news/detail-743022.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>測試PDF添加水印</h2>
<form method="post" action="http://localhost:8080/addPDFImageWaterMark" enctype="multipart/form-data">
<label for="srcFile">選擇PDF文件:</label>
<input type="file" name="srcFile" id="srcFile" accept=".pdf" required>
<br>
<label for="imagePath">選擇水印圖片:</label>
<input type="file" name="imagePath" id="imagePath" accept=".jpg, .png" required>
<br>
<button type="submit">添加水印并下載</button>
</form>
</body>
</html>
到了這里,關(guān)于java實現(xiàn)pdf文件添加水印,下載到瀏覽器的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!